Fed03
Fed03

Reputation: 585

Querying an async has Many Ember realtionship doesn't trigger any ajax request.

The title is self explanatory but what it's happening is that, when I go on the view route no Ajax request is made.

Moreover using the ember inspector component for chrome dev tools, in the data section, the message model has 0 items.

I've these ember versions as below:

DEBUG: -------------------------------
DEBUG: Ember      : 1.4.0
DEBUG: Ember Data : 1.0.0-beta.7.f87cba88
DEBUG: Handlebars : 1.3.0
DEBUG: jQuery     : 1.10.2
DEBUG: -------------------------------

In this code I'm using fixtures to represent the json that came back from my backend but the result is the same using fixtures or the RESTadapter.

App.Router.map(function() {
    this.resource('threads', { path: '/' }, function() {
        this.route('view', { path: ':thread_id' })
    });
});

App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.ThreadsRoute = Ember.Route.extend({
    model: function() {
       return this.store.find('thread');
    }
});

App.ThreadsViewRoute = Ember.Route.extend({
    model: function(params) {
       return this.store.find('thread', params.thread_id).get('messages');
    }
});

App.Message = DS.Model.extend({
    body: DS.attr('string'),
    thread: DS.belongsTo('thread')
});

App.Thread = DS.Model.extend({
    messages: DS.hasMany('message')
});

App.Thread.FIXTURES = [
    {
        id: 7,
        messages: [1, 4, 7, 8]
    }
];

App.Message.FIXTURES = [
    {
        id: 1,
        body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. A, voluptas, ducimus, temporibus neque adipisci culpa veniam nostrum harum voluptates impedit similique doloribus repudiandae suscipit atque vitae quisquam dolorum libero sequi?',
        thread: 7
    },
    {
        id: 4,
        body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. A, voluptas, ducimus, temporibus neque adipisci culpa veniam nostrum harum voluptates impedit similique doloribus repudiandae suscipit atque vitae quisquam dolorum libero sequi?',
        thread: 7
    },
    {
        id: 7,
        body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. A, voluptas, ducimus, temporibus neque adipisci culpa veniam nostrum harum voluptates impedit similique doloribus repudiandae suscipit atque vitae quisquam dolorum libero sequi?',
        thread: 7
    },
    {
        id: 8,
        body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. A, voluptas, ducimus, temporibus neque adipisci culpa veniam nostrum harum voluptates impedit similique doloribus repudiandae suscipit atque vitae quisquam dolorum libero sequi?',
        thread: 7
    }
];

Thanks in advance.

Upvotes: 1

Views: 299

Answers (1)

Cyril Fluck
Cyril Fluck

Reputation: 1581

The fixture adapter doesn't perform any XHR requests as all the data are already available in the app/page.

For the fact that you don't see any records in Ember Inspector, I've seen the problem when installing the extension through the web store if you are not using the right version of Ember and Ember Data. I don't see any problem with the following updated jsbin.

I've updated the jsbin to make it more ember idiomatic http://emberjs.jsbin.com/lukamuya/4

  • In order to make your life easier with Ember, you should follow the naming conventions (ThreadsViewRoute is in fact ThreadMessagesRoute).
  • Nesting resources are not implemented the way you did it. Check this.resource('threads') and this.resource('thread').

One issue in your JsBin, is that you use ThreadsView with an ID in the url. As you don't specify the ThreadsViewController, Ember thinks that you will return a single object and uses Ember.ObjectController and not Ember.ArrayController. In the model hook for this route, you return an array of messages which confuses Ember.

The other one is that store.find returns a promise. You can't write

store.find('thread', '7').get('messages')

but instead

store.find('thread', '7').then( function( thread) { return thread.get('messages'); })

EDIT: based on your comment, I've updated the JsBin http://emberjs.jsbin.com/lukamuya/9 so that /threads/1 shows the list of messages of the thread 1.

Upvotes: 1

Related Questions