Chen さん、正直なところ、 ember-data なしでデータを処理するのは非常に簡単です。私はそれを数回使用しましたが、そのうちのいくつかは非常に大規模なフロントエンド システムの構築に関連していました。その主な理由は、ember-data が当時それほど安定しておらず、あまり自信が持てなかったからです。もう 1 つの理由は、ご覧のとおり、非常に単純であるということです。
サーバーからデータを取得し、サーバーにデータを送信するクラスを作成するだけです (シングルトンの 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);
}
});
}
});