简体   繁体   中英

How to integrate a custom framework into backbone?

I access my server using PHP and this object literal form. This object literal is converted to JSON and sent to the server where it is converted back to an object. From there I read the model property to know which model to run. Also, contained is the user token, so I know which user to retrieve model data for.

$A.Packet.define({
    model:   null,
    client:  {},
    server:  {
        smalls:   {},
        feed:   {},
        favorites: {}
    }
});

Basically the strategy is encode information into an object literal, json encode it, send it, decode it, apply server logic and send back the data in JSON form.

This is all abstracted into running

$A.Packet.push({model: self.Name});

and then pre(packet) and post(packet) ajax methods run automatically.

How can I integrate this style into backbone?

According to Backbone I can over-ride the sync() function but I want to understand best how to integrate things before hacking a way.

My code has already been converted to MV* form but I'm using dummy data to test the front-end. I want to bring the back-end up now.

The dev site is here:

http://www.arcmarks.com/dev

Related

How to override Backbone.sync? - However I don't want to use localStorage yet and I have no use for the 4 methods it states I must implement.

All of my operations are POST JSON calls which do what they need once they hit the server.

In the annotated source of Backbone.LocalStorage there is a static method towards the end of the document called Backbone.LocalStorage.sync aliased to Backbone.localSync . This is the actual part they write the code that implements the custom sync.

  • They then reference Backbone.sync to Backbone.ajaxSync .
  • The next step implements a strategy pattern to decide which sync method to use.
  • Finally they overwrite Backbone.sync with a custom function to use the strategy.

This allows the developer to choose which sync type by adding properties to a models root object, in this case localStorage : new Backbone.LocalStorage() . You would just need to implement this for your own needs.

// Create your own sync method
Backbone.customSync = function(method, model, options, error) {
    // Your custom sync code here
    return $.post('http://something.somewh.ere', model.toJSON());

    // Implementing a switch statement for the method in here would
    // be a good idea :-)
}

// Create reference to original sync method
Backbone.ajaxSync = Backbone.sync;

// Get the sync strategy
Backbone.getSyncMethod = function(model) {
    if(model.customSync || (model.collection && model.collection.customSync))
        return Backbone.customSync;

    return Backbone.ajaxSync;
};

// Overwrite Backbone.sync to call getSyncMethod() to run sync tasks
Backbone.sync = function(method, model, options, error) {
    return Backbone.getSyncMethod(model)
                   .apply(this, [method, model, options, error]);
};

Now when your models have a property customSync that is a truthy value, your new sync strategy will be used.

This can all be adapted to specifically suit your needs, hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM