ember.js - Ember pattern for 'new' routes -


i have form visitors can leave comment typical "i accept tos" checkbox , link. link transitions new route tos text.

i create comment record when route form loaded. template can load right default settings (there gender radio button example set right value ember data attrs).

app.commentroute = ember.route.extend({   model: function() {     return this.store.createrecord('comment');   }   actions: {     create: function(comment) {       comment.save();     }   } }); 

the comment model:

app.comment = ds.model.extend({   body: ds.attr("string"),   gender: ds.attr("string", {defaultvalue: "m"}), }); 

and template:

<form {{action "create" on="submit"}}>   ... </form> 

now, when people click button tos page form fields emptied new model loaded. want behavior once form saved or if reload not when using button. worked around keeping global state feel there should better way. tips?

there numerous ways go this, expect few other answers. wouldn't start out route new record...

i reserve naming convention (comment) route gets particular model id... ideally, on route called app.commentnewroute. yeah, whatever works you.

here's 1 way might it:

app.commentroute = ember.route.extend({   model: function() {     if(ember.isempty(this.modelfor('comment'))){       return this.store.createrecord('comment');     } else {       return this.modelfor('comment');     }   }   actions: {     create: function(comment) {       comment.save();       this.set('model', this.store.createrecord('comment'));     }   } }); 

feels little clunky, should move along current approach. checking see if model exists. if does, return model may have changed.

when create comment, save , reset model new record.

good luck!


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -