Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

ResourceController findAll() 404 on resourceUrl. #5

Closed
fastone opened this Issue Feb 27, 2012 · 13 comments

Comments

Projects
None yet
5 participants

fastone commented Feb 27, 2012

When calling the ResourceController findAll() function, it doesn't find the resourceType's resourceUrl and thus gives 404 error on '/undefined' for the ajax call.

Owner

dgeb commented Feb 27, 2012

Please put a breakpoint here:

https://github.com/cerebris/ember-rest/blob/master/src/ember-rest.js#L239

And then in your console, evaluate this.get('resourceType') and then this.get('resourceType').prototype.resourceUrl

fastone commented Feb 27, 2012

this.get('resourceType') returns 'App.Contact'

this.get('resourceType').prototype.resourceUrl returns undefined.

Owner

dgeb commented Feb 27, 2012

Are you setting your resourceType in your controller to the string "App.Contact"? If so, set it directly to App.Contact.

fastone commented Feb 27, 2012

I've found that when I do an App.Contact.create(); before calling the findAll(); it loads the resourceUrl and the bindings also work.

So, on initial load of the application I should co an create(); on all models?

Owner

dgeb commented Feb 27, 2012

Your models should just be defined with Ember.Resource.extend() - it shouldn't be necessary to create them before calling findAll(). Perhaps this is a dependency load order issue?

When you hit that breakpoint above, evaluate App.Contact.isClass - it should return true. If not, make sure that your model class has been evaluated before your controller.

fastone commented Feb 27, 2012

I'm having this as sample code to test Ember, and see how thins work.

var App = Em.Application.create();

/* MODEL */
App.Contact = Ember.Resource.extend({
  resourceUrl: '/contacts.js',
  resourceName: 'contact',
  resourceProperties: ['first_name', 'last_name'],

  fullName: Ember.computed(function() {
    return this.get('first_name') + ' ' + this.get('last_name');
  }).property('first_name', 'last_name')
});

/* CONTROLLERS */
App.contactsController = Ember.ResourceController.create({
  resourceType: App.Contact
});

/* VIEWS */
App.ListContactsView = Ember.View.extend({
    contactsBinding: 'App.contactsController',

    init: function() {
        this._super();
//      this.set('contact', App.Contact.create());
        App.contactsController.findAll();
    },

    refreshListing: function() {
        App.contactsController.findAll();
    }
});
Owner

dgeb commented Feb 27, 2012

I assume you're working from https://github.com/dgeb/ember_rest_example or the corresponding blog post.

Have you tried running that example directly?

I tried moved your code into that example and commented out the corresponding code. The only changes I needed to make were to the resourceUrl in the model and adding a templateName to the view (see below):

var App = Ember.Application.create();

/* MODEL */
App.Contact = Ember.Resource.extend({
  resourceUrl: '/contacts',
  resourceName: 'contact',
  resourceProperties: ['first_name', 'last_name'],

  fullName: Ember.computed(function() {
    return this.get('first_name') + ' ' + this.get('last_name');
  }).property('first_name', 'last_name')
});

/* CONTROLLERS */
App.contactsController = Ember.ResourceController.create({
  resourceType: App.Contact
});

/* VIEWS */
App.ListContactsView = Ember.View.extend({
  templateName:    'app/templates/contacts/list',
    contactsBinding: 'App.contactsController',

    init: function() {
        this._super();
//      this.set('contact', App.Contact.create());
        App.contactsController.findAll();
    },

    refreshListing: function() {
        App.contactsController.findAll();
    }
});
Owner

dgeb commented Feb 27, 2012

@fastone - I pushed out some major changes today, including a full set of tests. I'm still not clear on the nature of this problem and have been unable to reproduce it in testing.

I'm going to close this for now, but please re-open it if you have a project, a jsfiddle, or any other test you can share that illustrates the problem.

@dgeb dgeb closed this Feb 27, 2012

@dgeb
Even I faced this issue while working on my project here:
https://github.com/karthikkck/rails-emberjs

I just loaded models before controller as shown below:
//= require_tree ./models
//= require_tree ./controllers

and this solved the issue.

Owner

dgeb commented Jun 11, 2012

@karthikkck - Thanks for the heads up as to what might have caused this problem. Ordering dependencies is certainly important.

@dgeb welcome. And thank you for giving the solution in previous comments. :)

@karthikkck - Thanks as well. The wrong order also causes "this.get("resourceType") is undefined" on the controller, because the model class has yet to be defined. Just a heads up in case anyone gets this error instead of yours, same root cause.

That's what I get for cheating in application.js with "//= require_tree .app", which is bad.

mck- commented Sep 20, 2012

ironically, the last line of this whole thread fixed my issue..

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