在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
问题描述
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屋!