then method doesn't work for getting hasMany relationships after saving #1276

Closed
xenyou opened this Issue Sep 11, 2013 · 7 comments

Comments

Projects
None yet
5 participants

xenyou commented Sep 11, 2013

Here is my code.
If you clicked a user name, you can see 'get("favorites") resolved' in console at the first time. But second time, you only see 'action!!'.

I thought it's related to #1177 but the issue closed because of another issue(#1269).
This happend even if the adapter is not FixtureAdapter(i.e. default RESTAdapter).

App = Ember.Application.create({ });
App.Router.map(function() { this.resource('users', {path: '/'}); });

App.UsersRoute= Em.Route.extend({
  model: function() { return this.store.find('user'); },
});

App.UsersController = Em.ArrayController.extend({
  actions: {
    getRel: function(user) {
      console.log('action!!');
      user.get('favorites').then(function(favorites) {
        console.log('get("favorites") resolved.');

        /* if this code block were removed, it works */
        user.save().then(function() {
          console.log('save done');
        }, function() {
          console.log('save failed');
        });

      }, function(reason) {
        console.log('rejected');
      });
    }
  }
});

App.User = DS.Model.extend({
  name: DS.attr('string'),
  favorites: DS.hasMany('favorite', {async: true}),
});

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

Em.TEMPLATES['users'] = Em.Handlebars.compile(
  '{{#each controller}}' +
     '<p {{action getRel this}}>{{name}}</p>' +
  '{{/each}}'
);
Owner

wycats commented Sep 12, 2013

Would you mind making a JSBin that shows the issue. It's hard for me to understand.

xenyou commented Sep 12, 2013

http://jsbin.com/uCoxoLa/6/edit?html,js

Here is a Instruction.

  1. Click a username
  2. Check your console. You'll see 'actions!!', 'resolved.', and 'save done' at the first time.
  3. Click the same username
  4. Check your console. This time, you'll see only 'actions!!' in console.

It seems that then method doesn't work at all.

@xenyou What do you exactly want to do with this code ? If you want to load the related children, then this is done automatically. If you add to your hbs code the following, then you will see that the children are loaded (only John has a favorite):

{{#each controller}}
{{action getRel this}}>{{name}}
{{#each favorites}}
{{name}}
{{/each}}
{{/each}}

So you do not need seperate code to get the children or so. If in the hbs template children are rendered, ED will fetch the data for you. Always use Ember Inspector during development and check the XHR requests and the resulting models in Ember. The reason that nothing happens the second time you click on a name is because the favorites for that user are already loaded and thus no additional request is needed.

If you want to do something else, then please let us know.

Marc

xenyou commented Sep 12, 2013

@cyclomarc

What do you exactly want to do with this code ?

As my comment in the js file said, I wanted to push some exsisting favorite to the user.favorites originally.
(But the pushing code is omitted to simplify the problem.)
To do so, favorites needed to be loaded first.

The reason that nothing happens the second time you click on a name is because the favorites for that user are already loaded and thus no additional request is needed.

As you say it's natural that it doesn't send additional requests to the server if it's already loaded.
But if favorites were already loaded I think that user.get('favorites') should resolved immediately without communicating the server.

FYI if you remove the saving block, you'll see 'resolved.' message in console again.

spieker commented Sep 14, 2013

+1

Owner

wycats commented Sep 28, 2013

Ok I can see what the issue is here. I'll try to identify what the cause is.

wycats was assigned Sep 28, 2013

This was referenced Sep 29, 2013

Member

bradleypriest commented Nov 14, 2013

Let's move discussion to #1440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment