javascript - Service property not updating consistently -
so 1 may little involved. full app, if stumped , want run yourself, on github.
(if do, you'll need login, username , password in api/tasks/populate.rake
... don't tell else, k?)
i'm using ember state services keep track of isclean
state of editor component (which using hallo.js).
here's sample code, scenario, , problem i'm having it:
when content in editor changed, hallo.js fires hallomodified
event. in component's didinsertelement
hook, i'm attaching jquery event listener, sets isclean
property false, , logs console can check it's working:
jfedit = ember.component.extend editorservice: ember.inject.service('jf-edit') editorstate: ember.computed('page', -> @editorservice.statefor(@page) # page passed in component # in route template ).readonly() # ... didinsertelement: -> self = @ @$().on("hallomodified", -> console.log "modified" self.get('editorstate.isclean') = false console.dir self.get('editorstate') # -> logs: class -> __ember123456789:null, # isclean: false # in console (but have click on # isclean show value) ).hallo( # ... ) `export default jfedit` # full code @ https://github.com/clov3rly/joyfarm/blob/master/app/app/components/jf-edit.em # (written in emberscript, adaptation of coffeescript)
this seems work, editorstate.isclean = false
in console.
so then, when attempt transition away page, check editor state, in order prompt user save.
newpostroute = ember.route.extend model: -> @*.store.createrecord 'post', title: "new post" content: "content here." editorservice: ember.inject.service('jf-edit') editorstate: ember.computed('model', -> @editorservice.statefor(@model) ).readonly().volatile() actions: willtransition: (transition) -> model = @modelfor('posts/new') console.log "editorstate:", @get('editorstate.isclean') # -> logs true, after log in # file above logged false. console.dir @get('editorstate') # -> logs: class -> # __ember123456789:null # (the same number above) # isclean property # not in log statement # ... unless @get('editorstate.isclean') confirm("do want discard changes?") || transition.abort() # full code @ https://github.com/clov3rly/joyfarm/blob/master/app/app/routes/posts/new.em
so now, editorstate.isclean returning false (and not showing when logging object itself). however, first property of object has same key, i'm assuming ember id of sort?
the template looks this:
{{{jf-edit page=model save="save" class="blog editor"}}}
so, page
in component/jf-edit
file should same object model
in routes/new
file.
the service/state files pretty simple, if want see them, can find them in this gist (or in repo itself).
turns out problem goes away if store editorstate on controller, , this.controller.editorstate
in route willtransition action handler.
thanks grapho , locks on irc ;)
Comments
Post a Comment