简体   繁体   English

如何将自定义框架集成到骨干中?

[英]How to integrate a custom framework into backbone?

I access my server using PHP and this object literal form. 我使用PHP和此对象文字形式访问服务器。 This object literal is converted to JSON and sent to the server where it is converted back to an object. 该对象文字将转换为JSON,然后发送到服务器,然后在服务器中转换回一个对象。 From there I read the model property to know which model to run. 从那里,我阅读了model属性,以了解要运行哪个模型。 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. 基本上,策略是将信息编码为对象文字,然后对其进行json编码,发送,解码,应用服务器逻辑并以JSON形式发送回数据。

This is all abstracted into running 所有这些都被抽象为正在运行

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

and then pre(packet) and post(packet) ajax methods run automatically. 然后pre(packet)post(packet) ajax方法自动运行。

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. 根据Backbone的说法,我可以重写sync()函数,但是我想在破解方法之前最好地了解如何集成事物。

My code has already been converted to MV* form but I'm using dummy data to test the front-end. 我的代码已经转换为MV *形式,但是我正在使用虚拟数据来测试前端。 I want to bring the back-end up now. 我现在想提起后端。

The dev site is here: 开发站点在这里:

http://www.arcmarks.com/dev http://www.arcmarks.com/dev

Related 有关

How to override Backbone.sync? 如何覆盖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. -但是,我现在还不想使用localStorage,也没有使用它指出必须实现的4种方法。

All of my operations are POST JSON calls which do what they need once they hit the server. 我所有的操作都是POST JSON调用,它们在到达服务器后便会执行所需的操作。

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 . 在带注释的Backbone.LocalStorage源中,在文档末尾有一个静态方法,称为Backbone.LocalStorage.sync别名为Backbone.localSync This is the actual part they write the code that implements the custom sync. 这是他们编写实现自定义同步的代码的实际部分。

  • They then reference Backbone.sync to Backbone.ajaxSync . 然后,他们将Backbone.sync引用到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. 最后,他们使用自定义函数覆盖Backbone.sync以使用该策略。

This allows the developer to choose which sync type by adding properties to a models root object, in this case localStorage : new Backbone.LocalStorage() . 这允许开发人员通过向模型根对象(在本例中为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. 现在,当模型具有作为真实值的属性customSync ,将使用新的同步策略。

This can all be adapted to specifically suit your needs, hope this helps. 所有这些都可以进行调整以特别适合您的需求,希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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