AdamPflug / Knockout.Programmatic

jQuery Programmatic data-binding for KnockoutJS
8 stars 0 forks source link

foreach binding #1

Closed bazjapan closed 12 years ago

bazjapan commented 12 years ago

Hi Adam, thanks for putting this out. It's really useful. I'm wondering whether you are still using this with knockout2. I'm having some trouble using the foreach binding. Knockout gives me the following error:

parentBindingContext is undefined

data testArray : ko.observableArray([{ name: "a"}, { name: "b"}, { name: "c"}])

markup ul data-id="listtest" li data-bind="text: name">Hello /li /ul (tags stripped)

directive $("ul[data-id=listtest]", context).databind({ foreach : vm_local.testArray });

//error happening in this code knockout-2.0.0.debug.js (line 2788)

ko.renderTemplateForEach = function (template, arrayOrObservableArray, options, targetNode, parentBindingContext) { var createInnerBindingContext = function(arrayValue) { return parentBindingContext'createChildContext'; };

throws : parentBindingContext is undefined

Anyways, thanks

AdamPflug commented 12 years ago

There is a known issue with Knockout 2.0 that I'm working on because Steve added a few new undocumented parameters to the bindings API in order to support his new native template syntax. I'm almost done with a fix that will get foreach/template working again with jQuery.tmpl tempates under Knockout 2.0, I expect to have that done in the next few days.

It makes a little less sense to use programmatic binding with the native templates introduced in Knockout 2.0 to me. It seems like you could either do the loop in JavaScript, or call ko.applyBindings on the DOM node with the foreach attached to it, e.g:

ko.applyBindings(vm_local.testArray, $("ul[data-id=listtest]", context).get(0));

I'm still thinking through how I would implement programmatic bindings for the new native template engine, so If you can think of a way you'd like it to work let me know and I'll look into it.

On Sun, Jan 29, 2012 at 7:18 PM, bazjapan < reply@reply.github.com

wrote:

Hi Adam, thanks for putting this out. It's really useful. I'm wondering whether you are still using this with knockout2. I'm having some trouble using the foreach binding. Knockout gives me the following error:

parentBindingContext is undefined

testArray : ko.observableArray([{ name: "a"}, { name: "b"}, { name: "c"}])

  • Hello

$("ul[data-id=listtest]", context).databind({ foreach : vm_local.testArray });

//error happening in this code knockout-2.0.0.debug.js (line 2788)

ko.renderTemplateForEach = function (template, arrayOrObservableArray, options, targetNode, parentBindingContext) { var createInnerBindingContext = function(arrayValue) { return parentBindingContext'createChildContext'; };

throws : parentBindingContext is undefined


Reply to this email directly or view it on GitHub: https://github.com/AdamPflug/Knockout.Programmatic/issues/1

AdamPflug commented 12 years ago

I just pushed up an updated version that should support jQuery.tmpl in KnockoutJS 2.0. I'll let you know when I get things figured out with native templates..

bazjapan commented 12 years ago

Awesome stuff Adam. Much appreciated. I'll give it a go when I get back home.

bazjapan commented 12 years ago

In my excitement, I forgot to get back to you - worked a treat - thanks