ObjectProxy - how to forward method calls?


#1

ObjectProxy handles properties , I need it to handle method calls as well. Here’s some sample code. Ideally javascript would let me intercept unhandle method calls on an object. It doesn’t so I’d like to do the following. In AuthObjectProxy.init, iterate over all of the instance methods of ‘search_model’ and then create theses methods for the AuthObjectProxy instance so I can forward them on the ‘content’ object. How do I get the instance methods of search_model?

App.MyAuthRoute = Ember.Route.extend
  model: (params) ->
    App.AuthObjectProxy.create
      object_id: params.post_id
      search_model: App.Post`

App.AuthObjectProxy = Ember.ObjectProxy.extend
  init: ->
    @_super

    if App.Auth.get('signedIn')
      @set 'content', @get('search_model').find(@get('object_id'))
    else
      @set 'content', null
    App.Auth.on 'signInComplete', =>
      @set 'content', @get('search_model').find(@get('object_id'))
    App.Auth.on 'signOutComplete', =>
      @set 'content', null
  #TODO dynamically intercept method calls and forward onto object
  save: ->
    @get('content').save(arguments)

#2

So this didn’t work, call stack too big

init: ->
  @_super
  sc =  @get('search_model')
  while not Ember.isNone(sc)
    Ember.keys(sc.prototype).forEach (key) =>
      @[key] = ->
        @get('content')[key](arguments)
    sc = sc.superclass

#3

And I’m beginning to think this probably isn’t the best way to go about thing, maybe need to rethink how this is going to work.


#4

You can’t intercept method calls in the current version of JavaScript (this will be possible with proxies in ES6, which is currently starting to land in Chrome and Firefox).


#5

#6

#7