javascript - Ember 2, model.save() from component my-modal.hbs -
i have posts.hbs:
{{#each model |post|}} <a href="#" {{action 'openwritemodal' post}}> <h3>{{post.title}}</h3> {{post.text}} {{post.author.name}} </a> {{/each}}
then open write-modal.hbs:
{{input value=model.title size="40" escape-press='close'}} {{model.author.name}} {{input value=model.text size="70" escape-press='close'}} <button {{action 'close'}}>close</button> <button {{action 'save' model}}>save</button>
now, components/write-modal.js:
export default ember.component.extend({ actions: { close: function() { return this.sendaction('close'); }, save(model) { model.set("text", model.get("text")); model.save().then(this.sendaction('close')); } } });
here posts.js:
export default ember.route.extend({ model() { return this.store.findall('post'); }, actions: { openwritemodal(post) { return this.render('modal', { outlet: 'modal', into: 'application', model: this.store.findrecord('post', post.id, { reload: true }), controller: 'application' }); }, closewritemodal() { return this.disconnectoutlet({ outlet: 'modal', parentview: 'application' }); }, savewritemodal(model) { model.save(); }, ...... model(params) { return this.store.query('post', params); } });
in application.hbs:
{{outlet}} {{outlet "modal"}}
and modal.hbs:
{{write-modal model=model close='closewritemodal' save='savewritemodal'}}
but got error: "uncaught typeerror: cannot read property 'save' of undefined"
how fix this?
you sending "post" record argument
{{#each model |post|}} <a href="#" {{action 'openwritemodal' "----post----"}}> <h3>{{post.title}}</h3> {{post.text}} {{post.author.name}} </a> {{/each}}
this ember record. , you're trying find exact same post
instead of using (u need specify "directory/model_name" not ds record instance)
model: this.store.findrecord('post', post.id, { reload: true }),
use
return this.render('modal', { outlet: 'modal', into: 'application', model: post controller: 'application' });
not sure if helps problem, let me know.
Comments
Post a Comment