How to re-render after modifying the model data?


#1

Hi, there. I’m confused with this situation: how to change the model data and show the new model smoothly?

I wrote some prototype codes. Here is the code: http://jsbin.com/tesatuwidi/1/edit?html,js,console,output

The result is strange. As you can see, when the addItem functions is triggered, the model data can be modified successfully, according to the console output. However, this change cannot be shown in the web page, just like nothing happened.

Has anyone met this before?


#2

I think you need to call addObject instead of push to get it to work.


#3

Thanks a lot. I changed from array.push() to array.addObject(), and it really works!

Thanks again!


#4

Why should I need to follow this approach? Am I wrong or Ember should update all elements bound to a model, automatically?


#5

As I understand it, it’s a way for ember to observe array changes. I’m guessing because there is no elegant way to observe changes when push is used.


#6

push() is a native JS array method. As the ember guys didnt want to override native methods, they extended the array with the addObject() method, so they could implement their own logic. At least thats what I think.