それでは、私たちはlocalStorage
は、データを格納および取得するために私たちの現実世界の実装では、アダプタのようなオブジェクトを表すであろうとStorage
オブジェクトと呼ばれてきたとしましょうオブジェクトの初期化時にいくつかのデータが格納されたダミー配列があることを確認してください。後でIndexRoute
model
フックを使用して検索します。アプリケーションの準備ができた後、より素敵なものに、あなたが直接register
& inject
を行うことができます今すぐ
が、我々は、それはアプリケーションの初期化時に既に利用になりたい場合はどう?まあ、そこにはemberの機能があります。Application.initializer
と呼ばれ、イニシャライザは 'name'プロパティとinitialize
という関数を持つシンプルなクラスで、アプリケーションにアクセスする場所はcontainer
です。コード内:
Ember.onLoad('Ember.Application', function(Application) {
// Initializer for registering the Storage Object
Application.initializer({
name: "registerStorage",
initialize: function(container, application) {
application.register('storage:main', application.Storage, {singleton: true});
}
});
// Initializer for injecting the Storage Object
Application.initializer({
name: "injectStorage",
initialize: function(container, application) {
application.inject('controller', 'storage', 'storage:main');
application.inject('route', 'storage', 'storage:main');
}
});
});
:アプリケーションは、我々はすべてのコントローラとすべてのルートになります
register
と
inject
前述
Storage
オブジェクト私たちの初期化子クラスを作成する
onLoad
イベントを聞くことができるのロードを開始したときに通知されるように
今、Storage
オブジェクトはすべてのルートとすべてのコントローラに注入されました。最後にIndexRoute
model
フックにアクセスして上記のストア配列を呼び出してself.get('storage').find('items')
というテンプレートを呼び出してレンダリングすることができます(実際にエンバーウェイというだけで、配列を返すよりも、いくつかの仮想遅れ、)と:
<script type="text/x-handlebars" id="index">
<h2>Index</h2>
<ul>
{{#each item in model}}
<li>Item: {{item}}</li>
{{/each}}
</ul>
</script>
:それはどこから来ているダミー配列が思いやりではない以上、我々はできる今agnosticallyループたちの
index
テンプレートで
App.IndexRoute = Ember.Route.extend({
model: function(){
var self = this;
var promise = new Ember.RSVP.Promise(function(resolve) {
Ember.run.later(function() {
var data = self.get('storage').find('items');
console.log(data);
resolve(data);
}, 1000);
});
return promise;
}
});
最後にここでは実例で説明したすべてをここで見ることができます:http://jsbin.com/eqAfeP/2/edit
希望します。