在ember中如何在Ember.Object.extend上创建可用于访问json服务的类方法 [英] in ember how to create a class method on Ember.Object.extend that can work to access a json service

查看:114
本文介绍了在ember中如何在Ember.Object.extend上创建可用于访问json服务的类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,问这样一个简单的问题,但我正在从jQuery迁移到Ember,并试图找出调用/响应json而不使用ember-data。我有一个问题是人们如何建议使用类方法。例如,我有一个这样的帖子对象:

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

一个合理的findById会是这样吗?

  $(document).ready(function(){
Hex.Post.findById = function(id){
console.log(你在这里 );
$ .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;
});
};
});

或者这是创建findById类方法的错误吗?



当我从chrome控制台运行它时,即使在一个浏览器中的JSON调用工作正常,它返回未定义。我究竟做错了什么?



thx



FROM CHROME CONSOLE:



解决方案

你需要在类上定义它,并返回ajax调用,那就是一个承诺。

  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;
});
}
});

使用承诺



从模型钩子,Ember将解决您的承诺,例如

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

作为承诺

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

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

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

这里有一个简单的例子:



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




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:

解决方案

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

这篇关于在ember中如何在Ember.Object.extend上创建可用于访问json服务的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆