ember.js - Bending Ember Data Records in Input Select -
with data structure:
app.publisher = ds.model.extend({ name: ds.attr('string'), books: ds.hasmany('book', {async: true}) }); app.book = ds.model.extend({ title: ds.attr('string'), summary: ds.attr('string'), author: ds.belongsto('author', {async: true}), publisher: ds.belongsto('publisher', {async: true}) };
in book edit
, book new
forms, i'd display publishers using <input type=select>
element.
my questions:
1 : how bind ember-data publishers records input element.
2: how select current book publisher in element edit or default new book
3: how bind selected publisher book, when submitting form
thanks lot.
you can use ember's built in select view of need here. bind list of publishers view's content may need list of publishers in route. example:
app.booknewroute = ember.route.extend({ var route = this; var modelpromise = new ember.rsvp.promise(function(resolve, reject){ route.store.find('publisher').then(function(result){ console.log("got publisher list"); // have new book model include list of publishers, , // selectedpub can used initial selection in // drop down list, , model attribute bound list var modelobj = {'publishers': result, 'selectedpub': result[0].get('id')}; resolve(modelobj); }); return modelpromise; });
now in template can use 'publishers' select view:
{{view "select" content=publishers optionvaluepath="content.id" optionlabelpath="content.name" value="selectedpub"}}
when user submits form, 'selectedpub' have id of publisher book. use id corresponding publisher model (e.g.):
bookcontroller.find('publishers').filterby('id', bookcontroller.selectedpub)[0] )
then set model publisher book.
Comments
Post a Comment