Skip to content

aaronksaunders/Alloy-StackMob-Adapter

Repository files navigation

#Appcelerator Alloy StackMob Sync Adapter

First attempt at migrating the old adapter from two months ago. This is an EARLY BASIC attempt and not meant to be a final solution, but my first draft and demostrating how this MIGHT be implemented... Enjoy... Learn and spread the word.

CODESTRONG

##To use stackmob sync adapter, you must set the keys in the tiapp.xml

<!-- stackmob keys -->
<property name="STACKMOB_PUBLIC_KEY" type="string">YOUR KEY</property>
<property name="STACKMOB_PRIVATE_KEY" type="string">YOUR PRIVATE KEY</property>

##Define your modules the usual manner

exports.definition = {

    config : {
        "columns" : {},
        "defaults" : {},
        "adapter" : {
            "type" : "stackmob",
        },
        "settings" : {
            STACKMOB_APP_NAME : 'people_interact',
            STACKMOB_USER_OBJECT_NAME : 'user',
            STACKMOB_MODEL_NAME : 'user',
        }
    },

    extendModel : function(Model) { 
        _.extend(Model.prototype, {
            url : function() {
                return "http://stackmob.mob1.stackmob.com/api/0/" + this.config.settings.STACKMOB_APP_NAME + "/";
            },
			/**
			* helper function to log a user into stackmob
			*/
            login : function(_opts) { debugger;
                var model = this;
                model.sync('login', model, {
                    success : function(data) { 
                        _opts.success && _opts.success(new Model(data));
                    },
                    error : function(data) {
                        _opts.error && _opts.error(data);
                    }
                });

            }
        });
        // end extend

        return Model;
    },

    extendCollection : function(Collection) {
        _.extend(Collection.prototype, {
            url : function() {
                return "http://stackmob.mob1.stackmob.com/api/0/" + this.config.settings.STACKMOB_APP_NAME + "/";
            },
        });
        // end extend

        return Collection;
    }
}

##Login a user, this is assuming you have created the user object in StackMob

var user = Alloy.createModel('User', {
    'username' : "alloy-sample-2",
    'password' : "password",
});
user.login({
    success : function(model, resp) {
        Ti.API.info(' user.login ' + JSON.stringify(model.toJSON()));
    },
    error : function(resp) {
        Ti.API.error(' user.login  ERROR ' + resp);
    }
})

##Create a User

var user = Alloy.createModel('User', {
    'username' : "alloy-sample-2",
    'password' : "password",
	'email' : 'info@clearlyinnovative.com'
});

user.save({}, {
    success : function(model, resp) {
        Ti.API.info(' user.login ' + JSON.stringify(model.toJSON()));
    },
    error : function(model, resp) {
        Ti.API.info(' user.login ' + JSON.stringify(model.toJSON()));
    }
})

##Objects will be created with a guid which Backbone needs but StackMob does not expose

{  
    "username": "alloy-sample-2",
    "lastmoddate": 1351720630379,
    "user_id": "55dd6f38-8e5e-c50f-e090-858d78c5565a", // Generated by adapter
    "sm_owner": "user/alloy-sample-2",
    "createddate": 1351720630379    
}  

the id is the combination of the object name + _id

About

Alloy-StackMob-Adapter

Resources

License

Unknown, Unknown licenses found

Licenses found

Unknown
LICENSE
Unknown
LICENSE.txt

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published