javascript - Why TypeError after adding properties to an observable object in knockout.js? -
i playing around building custom handler adds new observablearray-properties parts of myviewmodel make sure nested templates within elements bound handlers have right arrays - see jsfiddle full example.
<div data-bind="contentavailable: ['children','pos1']"> <div data-bind="template: { name: 'default', foreach: children().pos1 }"> </div>
within handler checked if current object bindingcontext.$data
had observable object property name valueaccessor()[0]
(in example 'children') , if observable object in turn had observable array of it's own name valueaccessor()[1]
('pos1') - , in case of absence assing them.
at first tried valuehasmutated() , reseting observable shown here
//within handler bindingcontext.$data[valueaccessor()[0]]()[valueaccessor()[1]]=ko.observablearray([]); //and after ko.applybindings(myviewmodel) child1.children().pos1().push(new viewchild(2)); child1.children().pos1.valuehasmutated();
this almost-worked - since didn't want notify mutation every time push value tried place valuehasmutated()
within handler - after adding observablearray()
result in uncaught typeerror: object not function
(tho typeof
1 line above says otherwise xd ).
am using object refrences in fundamentally wrong way or lacking understanding in how knockout it's magic? :d
still kind of digesting trying do, noticed have typo have i(1)
instead of i[1]
in typeof
check in binding handler:
if(!bc.$data[i[0]]().hasownproperty(i[1]) || typeof bc.$data[i[0]]()[i(1)] !== 'function'){ var existing = bc.$data[i[0]]();
Comments
Post a Comment