As mentioned in @andorov’s answer, the OP’s ideal code (<div style="width:{{model.width}}"> now pretty much just works as of Ember 1.10
I am new to Ember.js, and I am finding it difficult to dynamically
change CSS. Here is an example to explain what I mean:var App = Em.Application.create(); App.MyObj=Em.Object.extend({ objWidth:10 }); App.objController = Ember.ArrayController.create({ content: [], createObj: function(){ var obj = App.MyObj.create(); this.pushObject(obj); } });The code below doesn’t work, but it explains my goal. Using a
Handlebars template, I want to accomplish this:{{#each obj in App.objController}} <div style="width:{{obj.objWidth}}"></div> {{/each}}In other words, I just want to have the width of the
<div>update
when theobjWidthproperty is changed.
The
styleproperty has to be bound withbindAttr, passing the style as a whole, not just one of the properties, as it doesn’t understand that. This property only gives you access to the style as a string, targeting the immediate element. It’s not possible to bind to a property of the style if you define it like that.So here’s my sugestion: I’ve created a couple of “model” classes like the following, implementing a property that returns the width in pixels:
Then, for each item in your collection I’m binding that property to the
styleattribute:Follow the example with full code I’ve made at jsfiddle here http://jsfiddle.net/schawaska/ftWZ6/
Edit:
I’ve made some changes in the fiddle to add more feature, but the part to set the width stays the same.