ember.js - How to make an array of objects global in ember JS -
i have started learning ember js, question might sound silly. i'm trying design blog website using ember v 1.12.0. right now, i'm using static data posts. this:
var posts=[{ id:1, author:{name:"clark kent",age:32}, identity: "superman", description: "alien", date: new date('1-1-15'), body: 'stands hope' }, { id:2, author:{name: "bruce wayne",age:31}, identity:"batman", description: "billionaire", date: new date('1-1-16'), body: 'stands fear' }];
i make global can accessed multiple containers , data persists when edit details dynamically. know may not practice. but, right now, i'm trying build page. appreciated. thank you.
ideally use ember-data this, thats job designed do. need simple model defined data. take @ guides ember models.
if using ember-cli (which recommended), ember data included , need use command line create new model , add array fixture.
ember g model posts
your ember-cli model like:
let posts = ds.model.extend({ authorname: ds.attr('string'), authorage: ds.attr('string'), identity: ds.attr('string'), description: ds.attr('string'), body: ds.attr('string'), date: ds.attr('date') }); posts.reopenclass({ fixtures: [{ id:1, authorname: "clark kent", authorage: 32, identity: "superman", description: "alien", date: new date('1-1-15'), body: 'stands hope' }, { id:2, authorname: "bruce wayne", authorage: age:31, identity:"batman", description: "billionaire", date: new date('1-1-16'), body: 'stands fear' }]; });
export default documenter
this model can loaded wherever with:
this.store.find('posts', 1);
otherwise need ember-data lib github or bower , create simple model:
app.applicationadapter = ds.fixtureadapter; app.documenter = ds.model.extend({ authorname: ds.attr('string'), authorage: ds.attr('string'), identity: ds.attr('string'), description: ds.attr('string'), body: ds.attr('string'), date: ds.attr('date') }); app.documenter.reopenclass({ fixtures: [{ id:1, authorname: "clark kent", authorage: 32, identity: "superman", description: "alien", date: new date('1-1-15'), body: 'stands hope' }, { id:2, authorname: "bruce wayne", authorage: age:31, identity:"batman", description: "billionaire", date: new date('1-1-16'), body: 'stands fear' }]; });
again approach can call:
this.store.find('posts', 1);
to data store.
this used in model hook of route:
app.postsroute = ember.route.extend({ model: function() { return this.store.find('posts', 1); } });
as aside, recommend looking @ ember-cli tutorials.
finally, if don't want ember way, or learn ember way right now, chuck json localstorage. if plan on using ember, invest time use ember, ember-data integrates nicely ember , comes host of advantages.
Comments
Post a Comment