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

我怕爱的太早我们不能终老 提交于 2019-12-14 02:55:57

问题


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.


回答1:


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')]


来源:https://stackoverflow.com/questions/27697459/how-do-i-pass-a-can-compute-into-a-can-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!