core-elements icon indicating copy to clipboard operation
core-elements copied to clipboard

core-list-dart: it´s not possible access variable (@observable) outside data attribute

Open supermuka opened this issue 10 years ago • 7 comments

Original (and more details) on: http://stackoverflow.com/questions/28544403/core-list-dart-how-access-variable-observable-outside-data-attribute

I'm trying to apply a filter through a variable (asDateTime) for a given column to the core-list-dart.

This variable is outside of the content data attribute (model). When I access this variable, an error is throws.

Exception: Uncaught Error: Error evaluating expression '(model.dataVencimento | asDateTime)': Class '_ListModel' has no instance getter 'asDateTime'. NoSuchMethodError: method not found: 'asDateTime' Receiver: Instance of '_ListModel' Arguments: []

Second comments on stackoverflow, in JS it´s works. Apparently this is a bug.

supermuka avatar Feb 16 '15 18:02 supermuka

core-list-dart: bindings in the template passed to the list are evaluated against _ListModel. In Polymer.js they are evaluated against the context where the List element itself was added.

I stumbled upon this when porting this Polymer example https://github.com/zoechi/contacts-app where I needed a workaround for the filter getUrl at https://github.com/robdodson/contacts-app/blob/master/app/elements/contacts-page/contacts-page.html#L62 (my version https://github.com/zoechi/contacts-app/blob/master/lib/elements/contacts-page/contacts-page.html#L62)

This isn't an issue for me because the workaround was easy enough but it's obvious that core-list and core-list-dart behave differently.

zoechi avatar Feb 16 '15 18:02 zoechi

We could possibly add a parent attribute or something to add support for this? I am really surprised that this works in polymer js though, for instance if you look at the documentation, only index, selected, and model should be valid variables within that template (and this is how it works in dart):

List item templates should bind to template models of the following structure:
{
  index: 0,           // data index for this item
  selected: false,    // selection state for this item
  model: {            // user data corresponding to data[index]
    /* user item data  */
  }
}

jakemac53 avatar Feb 17 '15 16:02 jakemac53

I was surprised as well. I think some more explicit way (like parent) would be better than it's currently in Polymer.js. because when you know what's going on (how the core-list-dart grabs the template and uses it) its unexpected that such bindings still works. From a more naive point of view it's probably expected. But I think it is quite handy for some use cases.

zoechi avatar Feb 17 '15 16:02 zoechi

Ya, I definitely agree that there should be some way to access the parent scope.

jakemac53 avatar Feb 17 '15 16:02 jakemac53

@sigmundch how do you feel about adding the parent property to _ListModel?

jakemac53 avatar Feb 17 '15 16:02 jakemac53

Interesting.

I'm totally in favor of adding something, I'm just not sure what that is. I might be missing something obvious here, but if we add the .parent in _ListModel, what would it's value be? In our polymer-expressions scopes, the scopes are separate from the models, so it might look like a model, but then I'm not sure if the normal lookup will work there (we actually need the polymer expressions engine to do the lookup for us?).

Another option: instead of replacing the model in the template instance, we create a new scope (basically convert _ListModel into an actual polymer expressions scope). This would be something different than the _ModelScope, since _ModelScope doesn't have a parent scope (see https://github.com/dart-lang/polymer-expressions/blob/master/lib/eval.dart#L188).

Depending on what we do, this might require some changes in polymer-expressions to be able to have these new kind of scopes available.

sigmundch avatar Feb 17 '15 16:02 sigmundch

We could assign it to this.templateInstance.model, which I think gives us what we want

jakemac53 avatar Feb 17 '15 17:02 jakemac53