Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Ember = require('/lib/em_ti/ember-runtime');
- Keychain = require('com.obscure.keychain');
- /**
- @class
- An Appcelerator Cloud Services User.
- @extends Ember.Object
- */
- var User = Ember.Object.extend({
- // ..........................................................
- // Properties
- //
- /**
- Unique identifier for the user from ACS.
- @property {String}
- */
- id: null,
- /**
- Email used to log in to ACS.
- @property {String}
- */
- email: null,
- /**
- Access token used to sign in to ACS using Facebook.
- @property {String}
- */
- facebookAccessToken: null,
- /**
- Unique ID for user's Facebook account.
- @property {Number}
- */
- facebookId: null,
- /**
- Full name of the ACS user.
- @property {String}
- */
- name: null,
- /**
- Password used to log in to ACS.
- @property {String}
- */
- password: null,
- /**
- OAuth access token key for authorized Twitter user.
- @property {String}
- */
- twitterAccessTokenKey: null,
- /**
- OAuth access token secret for authorized Twitter user.
- @property {String}
- */
- twitterAccessTokenSecret: null,
- /**
- Unique ID for user's Twitter account.
- @property {Number}
- */
- twitterId: null,
- /**
- The name of the user as obtained from Facebook or Twitter or the username if name is not defined.
- @property {String}
- */
- displayName: function() {
- if (!Ember.none(this.get('name'))) {
- return this.get('name');
- } else {
- return this.get('username');
- }
- }.property('name', 'username'),
- // ..........................................................
- // Methods
- //
- /**
- Save the User to the local db.
- @returns {User} The saved version of the object.
- */
- save: function() {
- var db = Titanium.Database.open('mydb');
- Ti.API.info(this.toString());
- db.execute('CREATE TABLE IF NOT EXISTS users (id TEXT, json TEXT)');
- if (Ember.none(User.find(this.get('id')))) {
- db.execute('INSERT INTO users (id, json) VALUES (?, ?)', this.get('id'), this.toString());
- } else {
- db.execute('UPDATE users SET json = ? WHERE id = ?', this.toString(), this.get('id'));
- }
- db.close();
- if (!Ember.none(this.get('password'))) {
- var keychainItem = Keychain.createItem({ identifier: 'login' });
- keychainItem.account = this.get('username');
- keychainItem.valueData = this.get('password');
- }
- return this;
- },
- /**
- Returns the User object as a JSON object.
- @returns {Hash}
- */
- toJSON: function() {
- return this.getProperties('id', 'email', 'facebookAccessToken', 'facebookId', 'name', 'twitterAccessTokenKey', 'twitterAccessTokenSecret', 'twitterId');
- },
- /**
- Returns the User object as a stringified JSON object.
- @returns {String}
- */
- toString: function() {
- return JSON.stringify(this.toJSON());
- }
- });
- User.reopenClass({
- /**
- Override the default create method to handle creating from stringified JSON and arrays.
- @param {Object} options
- A JSON object, an Array of JSON objects, or a stringified JSON object to create the User object(s).
- @returns {Object} The created User object or an Array of User objects.
- */
- create: function(options) {
- if (Ember.none(options)) return this._super();
- if (typeof options === 'string') {
- try {
- var parsedOptions = JSON.parse(options);
- return this.create(parsedOptions);
- } catch(e) {
- Ti.API.warn('User#create could not parse the options string.');
- }
- } else if (options.constructor.toString().indexOf('Array') > -1) {
- var objArray = [];
- for (var i = 0; i < options.length; i++) {
- objArray.push(this.create(options[i]));
- }
- return objArray;
- } else {
- return this._super(options);
- }
- },
- /**
- Search the local db for a User for a specific id.
- @param {String} id
- The unique id for the User you're looking up as a string.
- @returns {Object} The User object or null if no User is found.
- */
- find: function(id) {
- var db = Titanium.Database.open('mydb'), user = null;
- db.execute('CREATE TABLE IF NOT EXISTS users (id TEXT, json TEXT)');
- var row = db.execute('SELECT * FROM users WHERE id = ?', id);
- if (row.isValidRow()) {
- user = this.create(row.fieldByName('json'));
- }
- row.close();
- db.close();
- return user;
- },
- /**
- Convert one or many User objects to JSON objects.
- @param {Object} user
- A User object or an array of User objects.
- @returns {Object} The JSON object or an Array of JSON objects.
- */
- toJSON: function(user) {
- if (!Ember.none(user) && user.constructor.toString().indexOf('Array') > -1) {
- var jsonArray = [];
- for (var i = 0; i < user.length; i++) {
- jsonArray.push(user[i].toJSON());
- }
- return jsonArray;
- } else {
- return user.toJSON();
- }
- }
- });
- module.exports = User;
Add Comment
Please, Sign In to add comment