3

I have very nested data, so I added a {{scope variable}}...{{/scope}} tag, which does nothing but changing the scope:

scope: function (newScope) {
    return this.tagCtx.render(newScope);
}

This works so far, but this sample code has an issue in the onAfterChange, which I use to push data back to the server.

In the initializer:

data.Foo.Bar.TheObject = new MyObject('Foo'); // has "Name" property set to "Foo"

In the template:

{{scope Foo.Bar.TheObject}}
     <input type="text" data-link="Name" />
{{/scope}}

It will display "Foo" correctly, but when I change it, in the onAfterChange I get the data object instead of TheObject. Can I add something to {{scope}} to achieve a scope change for the onAfterChange?
(The {{for}} tag has this behavior but I can't loop over a single object.)

neo post modern
  • 2,262
  • 18
  • 30
  • 1
    You say: "The {{for}} tag has this behavior but I can't loop over a single object". But it does not have to be an array. You can do {{for some.object}}... See http://www.jsviews.com/#fortag "Render the specified template for the given object, or iterate over the given array" – BorisMoore Dec 17 '13 at 00:12
  • Sorry, I apparently misunderstood the `{{for}}` tag then, thank you for clarifying. But for completeness sake: would it be possible to change the scope behavior of a tag? – neo post modern Dec 17 '13 at 10:42
  • I'll try to cover that scenario in documentation soon. If you want, can you create an issues on JsViews GitHub project concerning this, and also include the complete example above, or a jsfiddle illustrating what you think is not working as expected right now? Thanks – BorisMoore Dec 20 '13 at 17:29
  • So I spent quite some time figuring out [this fiddle](http://jsfiddle.net/St44L/5/) and now I am not sure if jsViews is actually doing wrong at all. The first approach (based on `event.type === "change"`) gets triggered exactly once, but requires what I call _Scoping_ when using `this.data`. The `"propertyChange"` does not require scoping if using `event.currentTarget` - but it fires twice. Which method should be prefered? Or did I misunderstand some part on how and where to call the Save-To-Server methods? – neo post modern Jan 03 '14 at 09:36
  • The changes made in commit 50 now allow you to use .observeAll to get full information on changes. onAfterChange is listening to changes on views, not on data, so one data change can certainly lead to more than one onAfterChange depending on how many view-bindings there are to the same data. You have "{^{:Foo.Bar.Name}}" and - hence two events. See http://www.jsviews.com/#observeAll. – BorisMoore Jan 22 '14 at 05:32

1 Answers1

0

As mentioned in comments above, the {{for}} tag does indeed cater to scenarios where you want to move the current data context (what mrs_sheep refers to as 'scope') to a different object (or array).

You can do {{for my.path.to.some.objectOrArray}} - and the target does not have to be an array.

See http://www.jsviews.com/#fortag "Render the specified template for the given object, or iterate over the given array"

BorisMoore
  • 8,444
  • 1
  • 16
  • 27