knockout.js - Knockout SmartTag implementation details -



knockout.js - Knockout SmartTag implementation details -

i wanted know more smart dirty tag implementation in 1 of links came across.

<ul data-bind="foreach: items"> <li data-bind="css: { dirty: dirtyflag.isdirty }"> <span data-bind="text: id"></span> <input data-bind="value: name" /> </li>

ko.dirtyflag = function(root) { var result = function() {} var _initialstate = ko.observable(ko.tojson(root)); result.isdirty = ko.computed(function() { homecoming _initialstate() !== ko.tojson(root); }); homecoming result; }; function item(id, name) { this.id = ko.observable(id); this.name = ko.observable(name); this.dirtyflag = new ko.dirtyflag(this); } var viewmodel = function(items) { this.items = ko.observablearray([ new item(1, "one"), new item(2, "two"), new item(3, "three") ]); this.save = function() { alert("sending changes server: " + ko.tojson(this.dirtyitems)); }; this.dirtyitems = ko.computed(function() { homecoming ko.utils.arrayfilter(this.items(), function(item) { homecoming item.dirtyflag.isdirty(); }); }, this); this.isdirty = ko.computed(function() { homecoming this.dirtyitems().length > 0; }, this); }; ko.applybindings(new viewmodel());

i made few changes original post

here new fiddle want know more reason creating separate function object ko.dirtyflag. blog post says

making object function , adding computed observable function object. means have bind dirtyflag.isdirty(). when ko.tojs runs, see plain function , ignore it.

what mean ?

_initialstate = ko.observable(ko.tojson(root))

does statement in ko.dirtyflag re-evaluate observables in creator of object i.e. item ?

also real need _initialstate observable ?

_initialstate = ko.observable(ko.tojson(root)) execution fails if _initialstate not observable.

a brief note on actual reason structuring code way want know.

making object function , adding computed observable function object. means have bind dirtyflag.isdirty(). when ko.tojs runs, see plain function , ignore it.

what mean ?

that means making function, not included in info sending server. help in not sending lot of unnecessary info server since server might not care isdirty stuff. same way tojs ignores observables related info , gives plain re-create of object.

_initialstate = ko.observable(ko.tojson(root));

does statement in ko.dirtyflag re-evaluate observables in creator of object i.e. item ?

no, on actual property/object, in case item. know name param "root" confusing not whole viewmodel.

also real need _initialstate observable ?

if see actual imprementation, isdirty flag computed propety based on initialstate.

result.isdirty = ko.computed(function() { homecoming _isinitiallydirty() || _initialstate() !== ko.tojson(root); });

if not made observable, isdirty not evaluated every time object changes.

hope makes sense.

knockout.js

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -