How do I pass a can.compute into a can.Component?

2019-08-11 03:42发布

How can I pass computes into components, such that changing the value in selected in one component will affect a value in a different component.

Example

http://jsbin.com/feleko/1/edit?html,js,console,output

I'm trying to set it up so that selecting a value in the first select changes the options available in the second. I think listening for dom change events should be straightforward, but I don't seem to be getting a compute I can update, or have access to the parent scope in order to use an attribute name to update it. Likewise the max value isn't an active object that receives updates.

标签: canjs
1条回答
三岁会撩人
2楼-- · 2019-08-11 04:06

I've found an obtrusive way. can.mustache provides a data helper that puts the current context on the element's data.

<select {{data 'context'}} value="a">

Then in the init event I can capture the element data and assign it to the scope so it's available to scope functions.

events: {
  init: function(el, opt) {
    opt.scope.context = el.data('context')
  }
}

Looking up values is then possible if awkward.

this.context[this.attr('value')]
查看更多
登录 后发表回答