Reputation: 121
I'm having issues returning a result from a Batman.Request call. I've comment the area where I'm calling the return. I'm pretty sure I can't just call return there, but I can't seem to find any documentation that will point me in the right direction.
Model:
class Web.Inbox extends Batman.Model
@resourceName: 'inbox'
@persist Web.InboxStorage
@encodeTimestamps()
@classAccessor 'task_entries', ->
request = new Batman.Request
url: 'http://0.0.0.0:3000/inbox'
autosend: false
success: (response) ->
results = new Batman.Set
results.add(Web.TaskEntry.createMultipleFromJSON(response))
console.log(results) # The value I want is here!
return results # I assume the issue is here
request.send()
Controller:
class Web.InboxController extends Web.ApplicationController
routingKey: 'inbox'
index: (params) ->
task_entries = Web.Inbox.get('task_entries')
@set('task_entries', task_entries)
console.log(task_entries) # not the same value as I attempt to return
# in the request
Console log from the model (What I want):
Set {_storage: Array[1], length: 1, _batman: _Batman, constructor: function, isCollectionEventEmitter: true…}
_batman: _Batman
_storage: Array[1]
0: Array[15]
0: TaskEntry
1: TaskEntry
2: TaskEntry
3: TaskEntry
4: TaskEntry
5: TaskEntry
6: TaskEntry
7: TaskEntry
8: TaskEntry
9: TaskEntry
10: TaskEntry
11: TaskEntry
12: TaskEntry
13: TaskEntry
14: TaskEntry
Console log from the Controller:
Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
Any help is appreciated!
Upvotes: 0
Views: 49
Reputation: 1874
Not sure if you saw bradstewart's response in IRC, just in case, here it is again:
Web.Inbox.get('task_entries')
This accessor fires an AJAX request, but it's asynchronous. It doesn't return the value you're looking for because that value is still being loaded from the server. As Brad pointed out, the accessor returns the Batman.Request
object, not the value from the server.
What you could do is make the accessor return a Batman.Set
, then also use the accessor to fire a Batman.Request
which will load records into that Set. When the set is updated, your view will be updated too. For example:
@classAccessor 'task_entries', ->
@_entries = new Batman.Set
new Batman.Request
url: "/inbox"
method: "GET"
success: (data) =>
records = Web.TaskEntry.createMultipleFromJSON(data)
@_entries.add(records...) # if you're on master branch, you could use `@_entries.addArray(records)`
return @_entries
This will cause the accessor to return a Set, which is empty at first, but the Set will be populated by the Batman.Request
and your view will be updated.
(oops, cross-posted from gist)
Upvotes: 0