2016-05-29 110 views
0

假设视图绑定必须观察模型道具,我怎样才能从另一个状态的状态变化触发状态变化?例如,第二国可能是衍生道具还是儿童道具?&符州 - 如何从外部国家观察国家支柱变化?

+1

一个简单的方法是'listenTo'你的模型https://ampersandjs.com/docs/#ampersand-events-listento。你也可以在道具中定义你的第一个状态,并且创建第二个状态(如果你愿意的话)。然后在初始化过程中,您只需将第一个状态传递给构造函数。 – amberv

回答

0

正确的&符视图绑定必须遵守当前模型(&符号 - 状态)。

为了当前状态为“听”到另一国,则可以使用的数据类型“状态”,例如包括第二状态作为当前状态的道具:

import State from 'ampersand-state'; 

export default State.extend({ 
    __name: 'ConnectionState', 
    idAttribute: 'site_id', 
    props: { 
     site_id: ['any', true], 
     message_group_id: ['any', true], 
     message_group: ['state'], 

然后当初始化状态,设置有第二状态的状态道具:

let messageGroups = new MessageGroupsCollection(state.messageGroups); 
let connections = new ConnectionsCollection(state.connections); 

connections.forEach((model) => { 
    model.set('message_group', messages.get(model.get('message_group_id'))); 
}); 

现在您的视图(通过国家的支持),您可以绑定到其他国家,如:

bindings: { 
    'model.message_group.is_hired': { 
     hook: 'isHired', 
     type: 'toggle' 
    } 
}, 

现在,当您对message_group.is_hired进行更改时,它将反映在Connection绑定中。