我在 View 中有一个带有来自 Controller 的绑定的数组对象。当我更改控制器中的数组时,视图中的数组会正确更改。但我的视图模板似乎已损坏:http: //jsfiddle.net/secretlm/2C4c4/84/
当我尝试在视图中使用重新渲染方法时。我的模板没有损坏。这个小提琴效果很好:http: //jsfiddle.net/secretlm/2C4c4/85/
HTML:
<script type="text/x-handlebars" data-template-name="test">
<button {{action "changeContent" target="App.latController"}}> Change</button>
{{#each element in view.content}}
<!-- if openTag is true, <ol> tag is created -->
{{#if element.openTag}}
<ol>
{{/if}}
<li>{{element.teo.item.Name}}</li>
<!-- if closeTag is true, </ol> tag is created -->
{{#if element.closeTag}}
</ol>
{{/if}}
{{/each}}
</script>
Javascript:
App = Ember.Application.create({});
App.LatView = Ember.View.extend({
templateName: "test",
contentBinding: "App.latController.contentWithIndices",
onRerender: function() {
this.rerender();
console.log(this.get('content'));
}.observes('content')
});
App.latController = Ember.Object.create({
changeContent: function() {
this.set('model', []);
var model = this.get('model');
var obj1 = {
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test1",
"Name": "The name1"
}
};
var obj2 = {
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test2",
"Name": "The name2"
}
}
var obj3 = {
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test3",
"Name": "The name3"
}
}
var obj4 = {
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test4",
"Name": "The name4"
}
}
var obj5 = {
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test5",
"Name": "The name5"
}
}
model.pushObject(obj1);
model.pushObject(obj2);
model.pushObject(obj3);
model.pushObject(obj4);
model.pushObject(obj5);
},
model: [
{
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test1",
"Name": "The name1"
}
}
,
{
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test2",
"Name": "The name2"
}
}
,
{
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test3",
"Name": "The name3"
}
},
{
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test4",
"Name": "The name4"
}
},
{
item: {
"DisplayOrder": 1,
"Public": true,
"Description": "The test5",
"Name": "The name5"
}
} ],
contentWithIndices: function() {
var length = this.get('model').length;
return this.get('model').map(function(i, idx) {
return {
teo: i,
openTag: (idx % 4 == 0),
closeTag: ((idx % 4 == 3) || (idx == length - 1))
};
});
}.property('model.@each')
});
var view = App.LatView.create({});
view.append();
我们什么时候应该手动使用重新渲染方法?有没有办法在不重新渲染我的视图的情况下做到这一点?欢迎任何想法,在此先感谢。