陈,老实说,没有 ember-data 处理数据非常容易。我用过几次,其中一些与构建相当大的前端系统有关。主要原因是当时的 ember-data 并没有那么稳定,并没有激发太多的信心。另一个原因是,正如您将看到的那样,它非常简单。
您所要做的就是创建一个类(拥有一个单例 ember 控制器也很好),它将从服务器获取数据并将数据发布到服务器。数据格式应为 JSON,以使所有过程更轻松、更清晰。您可以通过使用简单的 ajax 调用来实现这一点,
function retrieveData(callback){
jQuery.ajax({
url: *yoururl*,
timeout: ajaxTimeOut,
success: function(data){
/* here you store this data somewhere
it could be helpful to have a callback that you call so that this class/controller
is not coupled with specific logic*/
if(callback!=null){
callback(data);
}
}
});
}
当关联的路由被调用或从该路由的控制器调用时,可以调用检索数据的函数。传递给前一个类/控制器的回调函数会将检索到的数据设置为控制器的某些属性,甚至可以是它的模型。简单的例子,
http ://emberjs.jsbin.com/AsOcAbU/1/edit
js
App = Ember.Application.create();
App.Router.map(function() {
// put your routes here
});
App.IndexRoute = Ember.Route.extend({
/*model: function() {
return ['red', 'yellow', 'blue'];
},*/
setupController:function(controller,model){
this.controllerFor('myJSON').findJSONData(function(data){
controller.set('model',data);
});
}
});
App.IndexController = Ember.ArrayController.extend({
});
App.MyJSONController = Ember.Controller.extend({
findJSONData:function(callback){
var data = ['red', 'yellow', 'blue'];/*this will come from the server with an ajax call i.e. $.ajax({...})*/
if(callback){
callback(data);
}
}
});
HBs
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
{{#if model}}
<ul>
{{#each item in controller}}
<li>{{item}}</li>
{{/each}}
</ul>
{{else}}
loading ....
{{/if}}
</script>
在完整应用程序的上下文中,您可能需要迭代 json 数据并从中创建 ember 对象。多亏了 ember,这也很简单,
http://emberjs.jsbin.com/oWetaDuH/1/edit
/*let's say you retrieve color objects from php*/
findJSONData:function(callback){
setTimeout(function(){
/*this will come from the server
with an ajax call i.e. $.ajax({...})*/
var data = [{color:'red'}, {color:'yellow'}, {color:'blue'}];
if(callback){
callback(data);
}
},2000);//mimic ajax call
}
/*and you need to use ember magic bindings, so you have your own objects to represent the domain model*/
App.MyColor = Ember.Object.extend({
color:null
});
/*then the code for retrieving will become*/
setupController:function(controller,model){
this.controllerFor('myJSON').findJSONData(function(data){
var myColors=[];
data.forEach(function(jsonColor){
myColors.pushObject(App.MyColor.create(jsonColor));/*this is all you need to create the ember object !!*/
});
controller.set('model',myColors);
});
}
和hbs
<script type="text/x-handlebars" data-template-name="index">
{{#if model}}
<ul>
{{#each item in controller}}
<li>{{item.color}}</li>
{{/each}}
</ul>
{{else}}
loading ....
{{/if}}
</script>
更新
如果需要在模型解决之前暂停路由,promises
则应按照指南中的说明使用
http://emberjs.com/guides/routing/asynchronous-routing/
在这种情况下,由于jQuery
ajax
函数返回一个 Promise,所以前面的例子看起来像,
http://emberjs.jsbin.com/mogicira/1/edit
js
App.IndexRoute = Ember.Route.extend({
model:function(controller,model){
return this.controllerFor('myJSON').findJSONData(function(data){
var myColors=[];
data.forEach(function(jsonColor){
myColors.pushObject(App.MyColor.create(jsonColor));
});
return myColors;
});
}
});
App.IndexController = Ember.ArrayController.extend({
});
App.MyJSONController = Ember.Controller.extend({
findJSONData:function(callback){
return $.ajax({url:""}).then(function(data){
data = [{color:'red'}, {color:'green'}, {color:'blue'}];
if(callback){
return callback(data);
}
});
}
});