2013-05-29 98 views
0

我在想我是否在這裏做了一些根本性錯誤,但我試圖讓模型在視圖中定義樣式屬性。因此,例如,該燼視圖使用該卡片模板,並從<div style="color: green">...</div>開始,該模板由模型屬性顏色支持。當我通過App.Card.find(2).set("color", "color: red").save()在其他地方更改它時,我期望模板更新值,但它什麼都不做。在模板中直接使用{{ bindAttr style model.color }}會保持同步的值,但是我有一個額外的ember-view div元素。屬性視圖中的綁定不會根據模型更改進行更新

http://jsfiddle.net/dbhWg/3/

的javascript:

App = Ember.Application.create(); 

App.Store = DS.Store.extend({ 
    adapter: 'DS.FixtureAdapter' 
}); 

App.Router.map(function() { 
    // put your routes here 
}); 

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
     return App.Card.find() 
    } 
}); 

App.Card = DS.Model.extend({ 
    color: DS.attr('string'), 
}); 

App.Card.FIXTURES = [{ 
    id: 1, 
    color: "color: green" 
}, { 
    id: 2, 
    color: "color: blue" 
}]; 

App.CardView = Ember.View.extend({ 
    templateName: "card", 
    attributeBindings: ['style'], 
    style: function() { 
     return this.get('controller.model.color') 
    }.property('controller.model'), 
    didInsertElement: function() { 
     App.Card.find(2).set('color', "color: red").save() 
     console.log(App.Card.find(2).get('color')) 
    } 
}); 

模板:

<script type="text/x-handlebars" data-template-name="card"> 
    <h1> HELLO THERE </h1> 
</script> 

<script type="text/x-handlebars"> 
    <h2> Welcome to Ember.js </h2> 
    {{outlet}} 
</script> 

<script type="text/x-handlebars" data-template-name="index"> 
    {{#each item in model}} 
     {{render "card" item}} 
    {{/each}} 
</script> 
+0

jsfiddle似乎與您的示例不匹配 – ianpetzer

+0

哎呀抱歉,把它從別人的小提琴中分出來,糾正了鏈接。 – weehuy

回答

1

你忘了加上dependecency的顏色在計算財產

style: function() { 
    return this.get('controller.model.color') 
}.property('controller.model.color'), 

Working Fiddle

據我所知,你不能使用 bindAttr更新CSS什麼 我建議你使用類代替,類定義爲如下:

.red{ 
    color: red; 
} 

.green{ 
    color: green; 
} 

.blue: { 
    color: blue; 
} 

更新燈具爲:

App.Card.FIXTURES = [{ 
    id: 1, 
    color: "green" 
}, { 
    id: 2, 
    color: "blue" 
}]; 

綁定的顏色class如下

App.CardView = Ember.View.extend({ 
    templateName: "card", 
    classNameBindings: ['color'], 
    color: function() { 
     return this.get('controller.model.color'); 
    }.property('controller.model.color'), 
    didInsertElement: function() { 
     App.Card.find(2).set('color', "red").save(); 
     console.log(App.Card.find(2).get('color')); 
    } 
}); 
+0

我簡化了顏色,以便更容易。在我試圖設定位置的真實情況中。所以實際屬性是一個像「left:100px; top:100px;」的字符串。所以CSS類的變化並沒有真正的工作。 正如我注意到

雖然工程。當視圖版本沒有時,猜測它是令人驚訝的,特別是當你注意到類的變化起作用時。如果有足夠多的人同意這個問題有點不一致,我會調查一下拉請求。 – weehuy

+0

你想添加或減去當前位置的像素? –

+0

是或在其他情況下任意設置移動元素的位置,將其視爲可拖動或其他內容。 – weehuy

相關問題