Created
March 7, 2017 20:39
-
-
Save imana97/b3f20141fbba4b84d59bc9fad7c5f629 to your computer and use it in GitHub Desktop.
parse live service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('coachApp').service("parseAngular", ['$q', '$filter', function ($q,$filter) { | |
// keep the live query out, so we don't create duplicate! | |
var liveQuery={}; | |
// parse 3 class | |
var Live = function (scope, child, query, live) { | |
/** | |
* private properties | |
*/ | |
var _query = query; | |
var _className = _query.className; | |
var _ParseClass = Parse.Object.extend(_className); | |
var _live = live || false; | |
var _scope = scope; | |
var _child = child; | |
var _loadStart = function () { | |
}; | |
var _loadEnd = function () { | |
}; | |
var _error = function () { | |
}; | |
/** | |
* set a function to load on loading start | |
* @param func | |
*/ | |
this.onLoadStart = function (func) { | |
_loadStart = func; | |
}; | |
/** | |
* set a function when the load ends | |
* @param func | |
*/ | |
this.onLoadEnd = function (func) { | |
_loadEnd = func; | |
}; | |
/** | |
* | |
* @param func | |
*/ | |
this.onError = function (func) { | |
_error = func; | |
}; | |
/** | |
* bind loads the retrieved files and start auto updating the model. | |
* @param opFunc | |
*/ | |
this.bind = function (opFunc) { | |
_loadStart(); | |
_query.find({ | |
success: function (objects) { | |
_loadEnd(); | |
if (typeof (opFunc)=='function'){ | |
objects=opFunc(objects); | |
} | |
_scope[_child] = objects; | |
_scope.$apply(); | |
}, | |
error: function (o, e) { | |
_loadEnd(); | |
_error(e.message); | |
} | |
}); | |
// check the live query object | |
if (typeof (liveQuery[_className]) !='undefined'){ | |
liveQuery[_className].unsubscribe(); | |
} | |
// subscribe to live query | |
liveQuery[_className] = _query.subscribe(); | |
// on create | |
liveQuery[_className].on('create', function (newObj) { | |
// push to scope | |
_scope[_child].push(newObj); | |
_scope.$apply(); | |
}); | |
// on enter (similar to create) | |
liveQuery[_className].on('enter', function (newObj) { | |
// push to scope | |
_scope[_child].push(newObj); | |
_scope.$apply(); | |
}); | |
// on update | |
liveQuery[_className].on('update', function (newObj) { | |
$filter('filter')(_scope[_child], {id: newObj.id})[0]=newObj; | |
_scope.$apply(); | |
}); | |
// on delete | |
liveQuery[_className].on('delete', function (newObj) { | |
_scope[_child].splice(_scope[_child].indexOf($filter('filter')(_scope[_child], {id: newObj.id})[0]),1); | |
_scope.$apply(); | |
}); | |
// on leave (similar to delete) | |
liveQuery[_className].on('leave', function (newObj) { | |
_scope[_child].splice(_scope[_child].indexOf($filter('filter')(_scope[_child], {id: newObj.id})[0]),1); | |
_scope.$apply(); | |
}); | |
}; | |
/** | |
* add a new object | |
* @param obj | |
*/ | |
this.add = function (obj) { | |
_loadStart(); | |
var newObj = new _ParseClass(); | |
for (var i in obj) { | |
newObj.set(i, obj[i]); | |
} | |
newObj.save({ | |
success:function(){ | |
_loadEnd(); | |
// do nothing, because it is real time. | |
},error:function(o,e){ | |
_loadEnd(); | |
_error(e.message); | |
} | |
}); | |
}; | |
/** | |
* update an object | |
* @param id | |
* @param obj | |
*/ | |
this.update = function (id, obj) { | |
var toUpdate=$filter('filter')(_scope[_child], {id: id})[0]; | |
for (var i in obj){ | |
toUpdate.set(i,obj[i]); | |
} | |
_loadStart(); | |
toUpdate.save({ | |
success:function () { | |
_loadEnd(); | |
}, error:function (o,e) { | |
_loadEnd(); | |
_error(e.message); | |
} | |
}); | |
}; | |
/** | |
* remove an object | |
* @param id | |
*/ | |
this.remove = function (id) { | |
var toDelete=$filter('filter')(_scope[_child], {id: id})[0]; | |
_loadStart(); | |
toDelete.destroy({ | |
success:function(){ | |
_loadEnd(); | |
},error:function(o,e){ | |
_loadEnd(); | |
_error(e.message); | |
} | |
}); | |
}; | |
/** | |
* get the class object | |
* @returns {void|Object} | |
*/ | |
this.getClass=function(){ | |
return _ParseClass; | |
}; | |
/** | |
* get a parse object by ID or return -1 if it does not exist in angular model. | |
* @param objectId | |
* @returns {*|number} | |
*/ | |
this.getObjectById=function(objectId){ | |
return $filter('filter')(_scope[_child], {id: objectId})[0] || -1; | |
}; | |
}; | |
/** | |
* return an instance of live class. | |
* @param s | |
* @param c | |
* @param q | |
* @returns {Live} | |
*/ | |
this.live = function (s, c, q) { | |
return new Live(s, c, q, true); | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment