Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Alloy and singleton models

$
0
0

Hi everybody, let me start by apologising for the long post.

I'm new to Alloy models and I'm trying to study them but I can't find a way to understand and make use of them.

For example, let's say we have a model that will hold the user details, accessible throughout the application. The user data doesn't have to persist since next time the application is loaded, another user can use it,

So I'm talking here about a singleton (can I call it?) model.

models/user.js

exports.definition = {
    config: {
        columns: {
            name: "TEXT"
        },
        adapter: {
            type: "sql",
            collection_name: "name"
        }
    },
    extendModel: function(Model) {
        _.extend(Model.prototype, {
            // extended functions and properties go here
            validate: function(attrs){
                for(var key in attrs){
                    var value = attrs[key];
                    if(key === 'name'){
                        if(value.length <= 0){
                            return "Name can't be empty!";
                        }
                    }
                }
            }
        });
        return Model;
    }
};

1st question:

  • Since this is going to be a singleton and no data needs to persist, do I have to specify the adapter type, collection_name and attributeId?
  • Do I also have to extend Model and Collection?

2nd question:

  • How to instantiate a singleton using this module?

alloy.js

Alloy.Models.user = Alloy.createModel('user');
// or
Alloy.Models.user = Alloy.Models.instance('user');
...and access it: controllers/otherfile.js
var userModel = Alloy.Models.user;
I'm a bit confused because I've seen both here on Q&A, but also on other websites explaining the use of Alloy Models.

3rd question:

controllers/otherfile.js

var userModel = Alloy.Models.user;
 
userModel.on('error', function(mode, error){
    Ti.API.info(error); 
});
 
function saveUserData(){
    userModel.set('name', $.textFieldUserName.value);
    Ti.API.info('Is valid? ' + userModel.isValid());
}
When saveUserData() is called, the model will set the name property and the validate function will in the module will evaluate the data sent. If the data is invalid (this case, no name entered) it will return an error message. Console
// [INFO] :   Name can't be empty!
// [INFO] :   Is valid? true
- Why isValid() true if it didn't pass the validation function?

4th question:

When I close and reopen controllers/otherfile.js, and call saveUserData() again without sending any value to the property name, the output is:

Console

// [INFO] :   Name can't be empty!
// [INFO] :   Name can't be empty!
// [INFO] :   Is valid? true
... and if a close and reopen it again and to the same, the output is:

Console

// [INFO] :   Name can't be empty!
// [INFO] :   Name can't be empty!
// [INFO] :   Name can't be empty!
// [INFO] :   Is valid? true
- Does this means that I'm getting multiple instantiations of the model?

Viewing all articles
Browse latest Browse all 8068

Trending Articles