Source: Trackmotion.js

require('./iData.js');
/**
* Class: Trackmotion
* @constructor
*/
function Trackmotion(t){
    this._init (t);
}
Trackmotion.prototype = Object.create(iData.prototype);
/**
* @param t {} 
* @return bool
*/
Trackmotion.prototype._init = function(t){
    this.t = t;
    this.title = "none";
};


/** 
 * Create
 * @param data {}
 * @param cb function(){}
  * */
Trackmotion.prototype.create = function (data, cb)
{
   this.save(data, cb);
};

/** 
 * Update
 * @param data {}
 * @param cb function(){}
  * */
Trackmotion.prototype.update = function (data, cb)
{
   this.save(data, cb);
};

/** 
 * Read a trackmotion id and when done callback(me)
 * @param id integer
 * @return nulll
 * */
Trackmotion.prototype.read = function (id, cb)
{
    var me = this;
    if (this.t.credentials.authKey){
	var xhr = this.t.tws.getTrackmotion(id);
        xhr.addEventListener('load', function(){
            var jsData = JSON.parse(this.responseText);
            if (jsData.errors) alert("Error: "+JSON.stringify(jsData.errors[0].message));
            else {
                me.merge(jsData);
	        if(cb) cb(me);
	    } 
        });	
    }
};

/** 
 * Save
 * @param data {}
 * @param cb {function} callback
 * */
Trackmotion.prototype.save = function (data, cb)
{
    var me = this;
    if (this.t.credentials.authKey){
	var xhr = this.t.tws.saveTrackmotion(data);
        xhr.addEventListener('load', function(){
            var jsData = {};
            try { jsData = JSON.parse(this.responseText); }
            catch(e) { jsData.errors = "Inexpected error: "+e}
 
	    if (jsData.errors) console.log("Error: "+JSON.stringify(jsData.errors[0].message));
            else {
                me.merge(data);
	        if(cb) cb(jsData);
	    } 
        });	
    }
};

/** 
 * Delete
 * @param data {}
 * @param cb function(){}
 * */
Trackmotion.prototype.delete = function (data, cb)
{
    var me = this;
    if (this.t.credentials.authKey){
	var xhr = this.t.tws.deleteTrackmotion(data);
        xhr.addEventListener('load', function(){
            var jsData = JSON.parse(this.responseText);
            if (jsData.errors) alert(jsData.errors);
            else {
	        if(cb) cb(jsData);
                me = {};
	    } 
        });	
    }
};

module.exports = {Trackmotion:Trackmotion};