0

I'm confused as to how to fix this and wanted your opinion. I've created a library modeled after underscore's way of working with node and browsers. Here's a summarized version of the code: https://gist.github.com/76121b90fb1ae392a4de

Note that I'm using a Mixin to override the sync method for certain classes. In this case, the Session class. This is because some of our endpoints are not restful but JSON RPC so I have to handle them differently (see line 51).

The config gets passed to the instantiation of MyLibrary (line 86). Those config variables are then accessible as, for example, window.mylibrary.area.jsproxi. That is fine... The problem is I can't figure out an elegant way to access the same attributes of the instance from within the mixin.sync method (lines 11 and 12 are examples of this).

Any ideas? I don't know how to get the instance of MyLibrary from within the library after instantiation. ANd I can't use the name I'm attaching to window because I dont' know what this name will be.

Thank you very much!

Luis

luisgo
  • 2,385
  • 4
  • 24
  • 30

1 Answers1

0

First off in terms of how one would approach using mixins in Backbone, take a look at the following solution: Proper way of doing view mixins in Backbone. this will make sure that your reference to this is correct at any given time.

secondly You're instantiating a new MyLibrary, which is a model

new MyLibrary({
  "area": {
    "ajax_synchronization_token": "sometoken",
    "jsproxy": "proxytoken"
  },
  "user": {
    "key": "somekey",
    "signature": "somesignature"
  }
});

and expect to access the values as such:

this.area.ajax_synchronization_token,
this.area.jsproxy

whilst how you should really be trying to access those values is

var area = this.get('area'); // it's a model after all
area.ajax_synchronization_token
area.jsproxy
Community
  • 1
  • 1
Vincent Briglia
  • 3,068
  • 17
  • 13
  • I've tried that but I think I'm trying to access window.mylibrary.area or other attributes BEFORE MyLibrary.initialize() is done and hence those are not available. – luisgo May 14 '12 at 22:50
  • Correction: I can access, for example, window.mylibrary.get("area") which returns the attribute but I can't access window.mylibrary.area which is an instance of Area that contains other stuff because (I believe) MyLibrary.initialize is not done. – luisgo May 14 '12 at 22:52
  • I think I'm making things more confusing by not having clarified that there is another class called Area() that I instantiate in the MyLibray.initialize and hangs off of mylibrary.area. – luisgo May 14 '12 at 22:56