1

我有一些观点,与他们各自的模型密切相关:

image.handlerbars:

URL:{{view Ember.TextField 
  valueBinding="EvEditor.imageModel.imgUrl" 
}}<br/>

text.handlerbars:

{{view Ember.TextField 
 valueBinding="EvEditor.titleModel.text" 
}}<br/>

我现在想创建一个包含许多图像和文本块的复合视图:

var container = Ember.ContainerView.create({
    childViews: ['firstView', 'secondView', 'thirdView', 'fourthView'],
    firstView: App.TextView,
    secondView: App.TextView,
    thirdView: App.ImageView,
    fourthView: App.ImageView
});

现在,就目前而言,每个 ImageView 和 TextView 都将包含相同的数据,而不是为每个分配不同的 ImageModel / TextView 实例。

构建这种东西的最佳方法是什么?我真的不希望创建视图的代码也必须知道需要将哪些属性传递到每个视图中,例如,是吗?

理想情况下,我会编写类似这样的伪代码:

var container = Ember.ContainerView.create({
    childViews: ['firstView', 'secondView', 'thirdView', 'fourthView'],
    firstView: {view=App.TextView, model=App.EmberObjectincomingData[0]},
    secondView: {view=App.TextView, model=App.EmberObjectincomingData[1]},
    thirdView: {view=App.ImageView, model=App.EmberObjectincomingData[2]},
    fourthView: {view=App.ImageView model=App.EmberObjectincomingData[3]},
});
4

1 回答 1

0

Use something like https://stackoverflow.com/a/10225570/466772 and then add to the CollectionView :

for( var i=0;i<data.req.length;i++ ){
   var type=data.req[i],model={};
    if ( type == 'image'){
      model=imageModel.fromChangeFormat( data.data[i] );
    }else if (type.indexOf('title') >-1 || type.indexOf('text') > -1 ){
      data.req[i]='text';
      model=Ember.Object.create({                   
        text    : data.data[i]
       });
    }
    window.App.controller.addItem( data.req[i]+'View', model );
}

where window.App.controller is

controller = Em.ArrayProxy.create({
    content: [],
    addItem: function(itemType,data) {
        this.pushObject(App.Item.create({
            itemType: itemType,
            data:data
        }))
    }
});

then each View template can refer to view.content.data.foo where foo is a property in the correct Model for that View.

If the Model needs to attach customer observers or event handlers to the View for each item, you can do that in MyView before returning the string of the view (the this context is the View).

于 2012-08-09T15:59:37.687 回答