Quantcast
Channel: Appcelerator Developer Center Q&A Tag Feed (mvc)
Viewing all articles
Browse latest Browse all 25

Alloy MVC - change database for model at runtime?

$
0
0

Hi folks What I would like to be able to do is use the Alloy MVC stuff - but be able to switch the database used for a given model as and when I need to.

So - using an example with books and libraries...

The alloy.js file:

Ti.App.currentDB = "Library1"; //or change to Library2 when you're feeling brave

book.js in the /models/ directory. Note the use of Ti.App.currentDB (set in alloy.js) as the database name

exports.definition = {
    config: {
        columns: {
            "title": "text",
            "author": "text"
        },
        adapter: {
            type: "sql",
            collection_name: "book",
            db_name: Ti.App.currentDB
        }
    },
    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;
    }
};
in the index.js file note how we're trying to change the db name to "Library2":
Ti.App.currentDB = "Library2";
 
var library = Alloy.Collections.book;
library.fetch();
library.each(function(book){
    Ti.API.info(book.get('title'));
});
No matter what I do at runtime - the database used is always the one set in the alloy.js file (in this case "Library1") - which leads me to believe that the database is set either at compile time, or on app startup.

Is there any way to change the database at runtime, so when the fresh call is made to Alloy.Collections.book - the model uses "Library2" instead of "Library1"?


Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>