Search code examples
javascripttitaniumtitanium-mobiletitanium-alloy

Titanium, Alloy use instance of a model in two windows


I am using Alloy Titanium and I wanted to do something like this :

I have a model, view and controller, this is the view 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>

this is the model - 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;
}

};

and my controller index.js that connects to a fake api and fills the instance of the model -

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();

Now let's say I wanted to make another view file .xml with a different window, how would I go about using the same instance of the post model in that window? P.S. I am pretty sure that the model instance I made is local, but I am interested in a solution about binding a model to more windows.


Solution

  • You can check the titanium docs which clearly explains about the global singleton instance of the model and I think you will be able to use it through out. Check out the Titanium doc words :

    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');
    

    Hope it gives some idea.