1

I was studying about Knockout.js and how it works with Magento 2.

Then I reached the topic: template integration, in the first sample of it I'm presented to this notation:

<div data-bind="template:'Namespace_Module/sample'"></div>

Which will try to render the template located at:

app/code/Namespace/Module/view/frontend/web/template/sample.html

It's working in Magento versions equals/lower than 2.1.

In greater versions it's throwing the following error:

Uncaught TypeError: Unable to process binding "template: function(){return 'Namespace_Module/sample' }"
Message: Cannot read property 'name' of undefined
    at RemoteTemplateEngine.makeTemplateSource (engine.js:55)
    at RemoteTemplateEngine.renderTemplate (engine.js:116)
    at executeTemplate (knockout.js:4886)
    at ko.dependentObservable.disposeWhen (knockout.js:4950)
    at evaluateImmediate (knockout.js:1737)
    at Object.ko.computed.ko.dependentObservable (knockout.js:1946)
    at Object.ko.renderTemplate (knockout.js:4942)
    at update (knockout.js:5080)
    at ko.dependentObservable.disposeWhenNodeIsRemoved (knockout.js:3004)
    at evaluateImmediate (knockout.js:1737)

I was trying to understand why it's happening looking for the change in github, but it's not clear to me:

https://github.com/magento/magento2/commit/d99043c097816388b2aeec1c23e10bf15e064b28#diff-9ec27f11f8253078a99c9d1e979a07f3

Now it requires a bindingContext (which contain the name attribute requested in exception) but I don't know how it's initialized.

0

I just came across this same exact issue while following Alan Storm's KnockoutJs integration article.

I saw the following on another one of his articles (https://alanstorm.com/knockoutjs_primer_for_magento_developers/):

<div id="one" data-bind="template:{'name':'hello-world','data':first}"></div>

I haven't gotten it working yet, but perhaps this is the way to initialize the name attribute? Hopefully someone with more knowledge of magento2 can chime in with a better answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.