Giter VIP home page Giter VIP logo

ci.alloy.adapter.two's Introduction

New and improved ACS Alloy Module

This is very early hacking, late night work with lots of help and insight from Russ and the Alloy Team. Doing this work to better familiarize myself with the platform and get stuff done quicker, faster and better for my clients.

simple stuff, login a user

// create a user
var aUser = Alloy.createModel('User');

// pass in username and password
aUser.login("testuserone", "password", {
    success : function(_user) {
	
	    // user model is returned
	    Ti.API.info(' SUCCESS ' + JSON.stringify(_user));
	    Ti.API.info(' model username ' + _user.get("username"));

        // access to session information
	    Ti.API.info(' stored session ' + _user.retrieveStoredSession());
    },
    error : function(_error) {
	    Ti.API.error(' ERROR ' + _error);
    }
});

creating an object, works just like the books demo provided; here is using the ACS Place object

First lets look at the changes I made to the model JSON file, this is for the places object

{
    "columns": {
        "active": "boolean"
    },
    "defaults": {},
    "adapter": {
        "type": "acs",
    },
    "settings": {
        "object_name": "places", 
        "object_method": "Places"
    }

}

and this is for the user object

{
    "columns": {
        "active": "boolean"
    },
    "defaults": {},
    "adapter": {
        "type": "acs",
    },
    "settings": {
        "object_name": "users",
        "object_method": "Users"
    }
}

if you notice, the main edit is setting the adapter to acs and then specifying the object name. I know there is a cleaner way to do this, ie derive it from the file name, but this is just me hacking!! I will get to that later

And finally this is how it works...

var params = {
	"name" : "Appcelerator Cloud Services",
	"created_at" : "2011-03-22T21:12:14+0000",
	"updated_at" : "2011-03-22T21:12:14+0000",
	"address" : "58 South Park Ave.",
	"city" : "San Francisco",
	"state" : "California",
	"postal_code" : "94107-1807",
	"country" : "United States",
	"website" : "http://www.appcelerator.com",
	"twitter" : "acs",
	"lat" : 37.782227,
	"lng" : -122.393159
}


function testPlaces() {
	
	var aPlace, places;
	
	// create a place object
	aPlace = Alloy.createModel('Place', params);
	// save the object
	aPlace.save();
	
	// create a collection
	places = Alloy.createCollection('Place');
	
	// set up a trigger to print out the results when complete
	places.on("fetch", function() {
		Ti.API.info(' places...' + JSON.stringify(places));
	});
	
	// fetch the data
	places.fetch();


	// can also fetch individual item
	aPlace = Alloy.createModel('Place', {
		id : "SPECIFY PLACE ID"
	});
	aPlace.fetch();
}

You are going to want to hop on over ro app/sync/acs.js to see the beginnings of the code for the adapter

The code for the user model is more interesting since I needed to extend the object to support all of the special case methods that the user object supports

function login(_login, _password, _opts) {
	var self = this;
	this.config.Cloud.Users.login({
		login : _login,
		password : _password
	}, function(e) {
		if (e.success) {
			var user = e.users[0];
			Ti.API.info('Logged in! You are now logged in as ' + user.id);
			
			// return a newly created user object here!!
			_opts.success && _opts.success(new model(user));
		} else {
			Ti.API.error(e);
			// sorry.. error message
			_opts.error && _opts.error((e.error && e.message) || e);
		}
	});
}

Object Searching and Querying

So we keep it pretty simple here and pass in the parameters as part of the options in data as a hash

create the user collection like you normally do

var userCollection = Alloy.createCollection('User');

Now we do a fetch, but we pass in the query string in as a parameter; we are saying do a full text search on the term UserTwo

	userCollection.fetch({
		data : {
			q : "UserTwo"
		},
		success : function(collection, response) {
			Ti.API.info('success ' + JSON.stringify(collection));
		},
		error : function(collection, response) {
			Ti.API.error('error ' + JSON.stringify(collection));
		}
	});

We pass in a success and error callback that will give us the (collection, response) as results

For

create the user collection like you normally do

var userCollection = Alloy.createCollection('User');

Now we do a fetch, but we pass in the query string in as a parameter; we are saying find all users with the last_name field of UserTwo

	userCollection.fetch({
		data : {
			where : JSON.stringify({
				"last_name" : "UserOne"
			})
		},
		success : function(collection, response) {
			Ti.API.info('success ' + JSON.stringify(collection));
		},
		error : function(collection, response) {
			Ti.API.error('error ' + JSON.stringify(collection));
		}
	});

We pass in a success and error callback that will give us the (collection, response) as results which is the same as the previous example. Please note that the where parameter is a JSON object that has been converted to a string

The rest of the model follows the same path; check it out and tell me what you think

ci.alloy.adapter.two's People

Contributors

aaronksaunders avatar

Watchers

James Cloos avatar Mostafa Elkady avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.