in ember how to create a class method on Ember.Object.extend that can work to access a json service

ember.js

Solution

You'd want to define it on the class, and return the ajax call, which is then a promise

Hex.Post = Ember.Object.extend({
   id: null,
   body: null
});

Hex.Post.reopenClass({
   findById: function(id) {
     return Ember.$.getJSON("/arc/v1/api/post/" + id).then(function(data){
       var post = Hex.Post.create();
       post.set('id', data.id);
       post.set('body',data.body);
       return post;
     });
   }
 });

Using the promise

from a model hook, Ember will resolve the promise for you, example below

Hex.PostRoute = Em.Route.extend({
  model: function(param){
    return Hex.Post.findById(param.id);
  }
});

as the promise

Hex.Post.findById(42).then(function(record){
  console.log(record);
});

or

var promise = Hex.Post.findById(42);

promise.then(function(record){
  console.log(record);
});

And here's a simple example:

http://emberjs.jsbin.com/OxIDiVU/21/edit

Problem

Sorry to ask such a simple question but I'm looking at migrating from jQuery to Ember and am trying to figure out calling / responding json without using ember-data. One question I have is how do people suggest having class methods. Say for example I have a post object like this: ``` Hex.Post = Ember.Object.extend({ id: null, body: null }); ``` Would a reasonable findById look like this? ``` $(document).ready(function(){ Hex.Post.findById=function(id){ console.log("you are here"); $.getJSON("/arc/v1/api/post/" + id, function(data){ var post = Hex.Post.create(); post.set('id', data.id); post.set('body',data.body); return post; }); }; }); ``` Or is this just wrong for creating a findById class method? When I run this from the chrome console, it comes back as undefined even though the JSON call works fine in a brower. What am I doing wrong? thx FROM CHROME CONSOLE:

Original source