2

What's the proper way to find item in the Ember.js ArrayController? I have set of contacts in the controller:

App.contactsController = Em.ArrayController.create({
    content:[],
});

There are objects in the controller, they are displayed and everything works fine. Then, I want to implement router with serialization/deserialization:

...
deserialize:function (router, params) {
    var contact = App.contactsController.find(function(item) {
        return item.id == params.contact_id;
    });
},
...

However, the find function does not appear to do any iteration. What could be the reason? Is it possible that the Router tries to do the routing before the application calls its ready method? That's the place I fill the controller with data.

EDIT: Well, I have found that router tries to make the transition before I fill my arrayController by the data (in Ember.Application.ready method). Is it possible to "delay" routing after the data is properly set?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Pavel S.
  • 11,892
  • 18
  • 75
  • 113

3 Answers3

0
var contact = App.contactsController.filter(function(item) {
    return item.id == params.contact_id;
});

I think you can run Application.initialize() when router has been set. You can use observer to detect data set.

dataChanged: function() {
    console.log(this.get('content.length'));
    // before emberjs 1.0pre
    // console.log(this.getPath('content.length')); 
}.observes('content')
0

The problem was actually caused by insertind data into arrayController after the Router did its deserialization. Putting it before App.initialize() solved the problem.

Pavel S.
  • 11,892
  • 18
  • 75
  • 113
0

the correct answer is:

var contact = App.contactsController.content.find(function(item) {
    return item.id == params.contact_id;
});

It will not return an item if you don't point to the content array.

WallMobile
  • 1,939
  • 1
  • 20
  • 35