BorisMoore / jsviews

Interactive data-driven views, MVVM and MVP, built on top of JsRender templates
http://www.jsviews.com/#jsviews
MIT License
857 stars 130 forks source link

'onError' breaks 'for' tag with external template and passing parameters #354

Closed trueqbit closed 7 years ago

trueqbit commented 7 years ago

I am trying to link a list of items to table-rows. There are two lists for different purposes, but with the exact same data structure, which is why I want to encapsulate markup for an item into an external template.

The list might be absent from the data, that's why I am using an onError handler. This is even more necessary because the list in turn is wrapped inside another optional container [note that the json is actually produced by x2js from xml].

Here is some simplified code to show what I am talking about:

var model = {
  persons: {
    /*person: [
      { name: 'name 1' },
    ]*/
  },
};

{^{for model.persons^person ~prefix='td' tmpl='#rowPartial' onError='fallback'/}}

The actual problem that occurs is that jsviews outputs 'fallback' instead of rendering the template.

Fixing the data structure, such that the 'persons' container is always present, is a possibility, but I believe this to be a bug in jsviews, because either removing 'onError' or the '~prefix' parameter makes things work again.

Here is a fiddle showcasing the issue.

BorisMoore commented 7 years ago

Thanks for finding this! Yes, it is a bug.

The fix is very simple. Change line https://github.com/BorisMoore/jsviews/blob/master/jsviews.js#L3620 to

sourceValue = linkFn(source, view, $sub);

I'll include the fix in the next update.

trueqbit commented 7 years ago

Thanks! The fix is working great, both for data-linked tags and elements.

BorisMoore commented 7 years ago

Fixed with commit 84