How to work with BelongsToProxy and instance methods

25 views
Skip to first unread message

roelof....@gmail.com

unread,
Jun 19, 2014, 12:21:34 PM6/19/14
to batm...@googlegroups.com
I'm experiencing some trouble getting some Batman models to work using a belongsTo relationship. I'm using Batman v0.16.

I have these two classes:

class CoconutMobile.Thumbnail extends Batman.Model
@belongsTo 'file', inverseOf: 'thumbnail'


class CoconutMobile.File extends Batman.Model
@hasOne 'thumbnail', inverseOf: 'file'

In have a thumbnail partial:

<div class="thumbnail" data-event-click="destroyFile | withArguments thumbnail.file">
<img data-bind-src="thumbnail.data">
</div>

and a controller method:

destroyFile: (file) ->
file.destroy()

However, this doesn't work. This last line of code throws an error because the destroy method is undefined. The controller method is called with a BelongsToProxy, which doesn't have a destroy() method. I can retrieve properties of the file object, if I use file.get('name'), it correctly returns the file's name. And if I use

file.get('target').destroy()

it works fine as well. But then the controller method would never work if the destroyFile method would be called with a plain File object. Shouldn't the proxy re-route these method calls to target? Or am I missing something?



Robert Mosolgo

unread,
Jun 19, 2014, 12:28:23 PM6/19/14
to batm...@googlegroups.com
Hi,

Thanks for sharing this issue.

I think you're right -- the proxy should route these calls to the target. Using belongsToProxies everywhere is pretty new (started in 0.16), which is why this hasn't popped up yet.

Let's track the issue here: https://github.com/batmanjs/batman/issues/1061

In the mean time, I recommend working around it by explicitly pointing to target:

<div data-event-click='destroyFile | withArguments thumbnail.file.target'></div>

That's what we're doing now to work around some of the ambiguity of belongsToProxy (eg, `data-showif='belongsTo.target`). How does that work for you?


robert


 









--
You received this message because you are subscribed to the Google Groups "batman.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to batmanjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Mosolgo

unread,
Jun 19, 2014, 1:23:58 PM6/19/14
to batm...@googlegroups.com, rdmo...@gmail.com
Wow, I literally just ran into this for the first time. I had a case where, sometimes I had a real object and sometimes I had a belongsToProxy. I worked around it like this:

<div data-context-realitem='item.target | default item'>
 
<a data-event-click='editItem | withArguments realitem'></a>
</div>

That way, `realitem` was always an item.



On Thursday, June 19, 2014 9:28:23 AM UTC-7, Robert Mosolgo wrote:
Hi,

Thanks for sharing this issue.

I think you're right -- the proxy should route these calls to the target. Using belongsToProxies everywhere is pretty new (started in 0.16), which is why this hasn't popped up yet.

Let's track the issue here: https://github.com/batmanjs/batman/issues/1061

In the mean time, I recommend working around it by explicitly pointing to target:

<div data-event-click='destroyFile | withArguments thumbnail.file.target'></div>

That's what we're doing now to work around some of the ambiguity of belongsToProxy (eg, `data-showif='belongsTo.target`). How does that work for you?


robert


 
I'm experiencing some trouble getting some Batman models to work using a belongsTo relationship. I'm using Batman v0.16.

I have these two classes:

class CoconutMobile.Thumbnail extends Batman.Model
  @belongsTo 'file', inverseOf: 'thumbnail'


class CoconutMobile.File extends Batman.Model
  @hasOne 'thumbnail', inverseOf: 'file'

In have a thumbnail partial:

<div class="thumbnail" data-event-click="destroyFile | withArguments thumbnail.file">
  <img data-bind-src="thumbnail.data">
</div>

and a controller method:

destroyFile: (file) ->
  file.destroy()

However, this doesn't work. This last line of code throws an error because the destroy method is undefined. The controller method is called with a BelongsToProxy, which doesn't have a destroy() method. I can retrieve properties of the file object, if I use file.get('name'), it correctly returns the file's name. And if I use

file.get('target').destroy()

it works fine as well. But then the controller method would never work if the destroyFile method would be called with a plain File object. Shouldn't the proxy re-route these method calls to target? Or am I missing something?







--
You received this message because you are subscribed to the Google Groups "batman.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to batmanjs+unsubscribe@googlegroups.com.

roelof....@gmail.com

unread,
Jun 19, 2014, 1:54:24 PM6/19/14
to batm...@googlegroups.com, rdmo...@gmail.com
Thanks for your quick response! Your last solution is similar to the workaround I'm using in the controller:

destroyFile: (file) ->
file = file.get('target') || file
file.destroy()

Not sure which is better, adapting the partial or the controller. Both solutions feel like a bit of a hack, the proxy objects should be able to handle this.

I've been experimenting a bit with creating Batman objects client side, to see if I can get the relationship to always return a BelongsToProxy. I'm not getting the proper results, but I have noticed that this is incorrect:

file = CoconutMobile.File.get('loaded.last')
thumbnail = new CoconutMobile.Thumbnail()
thumbnail.set('file', file)

because thumbnail.get('file') will return a CoconutMobile.File object.

Instead, when using

thumbnail.set('file_id', file.get('id'))

thumbnail.get('file') will return a BelongsToProxy object.

I am sure you already know this, but for others this might have some value to know.
> To unsubscribe from this group and stop receiving emails from it, send an email to batmanjs+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages