I assumed this will work in my BatmanJS Controller:
project = Project.find parseInt(params.id, 10), (err) ->
throw err if err
@set 'project', project
**@set 'owner_id', project.get("owner_id")**
but project.get("owner_id") is always undefined. Im wondering if my call with Project.find is wrong, since i thought it is quite similar to rails. What am i doing wrong?
owner_id is an attribute of the project and valid in the view:
%p{"data-bind" => "owner_id"}
works and spits out the correct owner id.
Like many things in JS,
Batman.Model.findis an asynchronous method, from the docs...So, here's the order of things as they're happening in your code...
Project.findwhich tellsBatman.Modelto call ourcallbackfunction after it has retrieved the data from the DB.'project'keypath to that promise.#geton that promise (before it has a value).findreturns from the DB and soprojectand therefore your'project'keypath are updated with the value of your project - but'owner_id'is still undefined.Fix this by not trying to set intermediate keypaths, but just directly binding to the attributes of your model in your views.
Ie. have this in your controller...
...and then this in your view...
When the project eventually arrives from the DB, that element will be updated with the
owner_idAlternatively, if you want to do something with that
owner_idvalue in your controller, then setup an observer for the keypath, that will be triggered when theprojectarrives from the DB, eg.So, when the
Project.findreturns it will set the'project'and in turn the'project.owner_id'keypaths, which will trigger your observer to go and retrieve theOwnerand set the'owner'keypath. So now you could have in your view...