Titanium,Alloy 使用一个模型实例中的两个 windows

Titanium, Alloy use instance of a model in two windows

我正在使用 Alloy Titanium,我想做这样的事情:

我有模型、视图和控制器,这是视图index.xml -

<Alloy>
<Model src="post" instance="true" id="postIns"/>
<Window class="container" onSwipe="update" model="$.postIns">
    <Label id="postTitle" top="15">{$.postIns.title}</Label>
    <Label id="postContent">{$.postIns.body}</Label>
    <Button id="updateButton" onClick="update" bottom="0">Zemi nov post</Button>
</Window>

这是模型 - post.js -

exports.definition = {
config: {
    "defaults": {   
        "userId": "",
        "id": "",
        "title": "Title",
        "body": "",
    },

    adapter: {
        type: "properties",
        collection_name: "post"
    }
},
extendModel: function(Model) {
    _.extend(Model.prototype, {
        // extended functions and properties go here
    });

    return Model;
},
extendCollection: function(Collection) {
    _.extend(Collection.prototype, {
        // extended functions and properties go here
    });

    return Collection;
}

};

和我的控制器 index.js 连接到一个假的 api 并填充模型的实例 -

var id = 1;

function update() {

    id =_.random(0, 50);

    var results = {};

    var client = Ti.Network.createHTTPClient({    
    //  called when the response data is available    
    onload : function(e) {        
        results = JSON.parse(client.responseText);        
        // display results on console        
        Ti.API.info(JSON.stringify(results,null,2));  
        // save the results to the instance
        $.postIns.save(results);  
        },    
        //  called when an error occurs, including a timeout    
    onerror : function(e) {        
        results = JSON.parse(client.responseText);        
        // display error results on the console        
        //Ti.API.err(JSON.stringify(results,null,2));    
        },
});

    var url = "http://jsonplaceholder.typicode.com/posts/" + id;

    client.open("GET", url);

    client.send();
}

$.index.open();

现在假设我想用不同的 window 制作另一个视图文件 .xml,我将如何在 post 模型中使用相同的实例 window? P.S。我很确定我制作的模型实例是本地的,但我对将模型绑定到更多 windows.

的解决方案很感兴趣

您可以查看 titanium 文档,它清楚地解释了模型的全局单例实例,我认为您将能够一直使用它。 查看 Titanium 文档的话:

You can also create a global singleton instance of a model, either in markup or in the controller, which may be accessed in all controllers. Use the Alloy.Models.instance method with the name of the model file minus the extension as the only parameter to create or access the singleton.

// This will create a singleton if it has not been previously created,
// or retrieves the singleton if it already exists.
var book = Alloy.Models.instance('book');

希望它能提供一些想法。