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.find
is an asynchronous method, from the docs...So, here's the order of things as they're happening in your code...
Project.find
which tellsBatman.Model
to call ourcallback
function after it has retrieved the data from the DB.'project'
keypath to that promise.#get
on that promise (before it has a value).find
returns from the DB and soproject
and 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_id
Alternatively, if you want to do something with that
owner_id
value in your controller, then setup an observer for the keypath, that will be triggered when theproject
arrives from the DB, eg.So, when the
Project.find
returns it will set the'project'
and in turn the'project.owner_id'
keypaths, which will trigger your observer to go and retrieve theOwner
and set the'owner'
keypath. So now you could have in your view...