-
Notifications
You must be signed in to change notification settings - Fork 0
Coq API
- Declaring a model
- Attributes "configuration object"
- Coq Finders
- CoqModel.find(value)
- CoqModel.all()
- CoqModel.where(query)
var MyModel = Coq.factory({
/* options */
$resource : $resource(/* ... */),
$attributes : { /* see "Attributes 'configuration object' " section */ },
$primaryKey : 'id',
/* custom instances methods */
setBusy : function() { /* ... */ },
/* custom static methods */
$statics : {
/* ... */
}
});
-
$resource
: ngResource object used as CRUD -
$attributes
: Object with key as attribute name and configuration object as value -
$primaryKey
: specify a attribute to use when do.find(/* value */)
If not present, firstconfig.$resource
route param will be used, example : Ifconfig.$resource
route is"/user/:id"
, doingUser.find(1)
will request/user/1
-
$statics
: static methods to add on model
You can also add any properties as you want to add instance method on your model.
- perform an
Model.find(/* value */)
on a Model without$primaryKey
or without "resource route param" will end with a promise rejection withreason
: "Error: unable to locate primary key"
var MyModel = Coq.factory({
/* ... */
$attributes : {
user_id : {
type : 'number',
visible : false
},
user_name : 'text'
},
/* ... */
});
Each attribute value can be an "config object" or a string.
Note: If Model configuration does not provide a $primaryKey
or provided $resource
does not have any "route param", a rejected promise will be returned with reason
: "Error: unable to locate primary key"
var MyModel = Coq.factory({
$resource : $resource('http://api.com/users/:user_id'),
$attributes : {
user_id : 'number',
/* ... */
},
/* ... */
});
// GET http://api.com/users/1
MyModel.find(1);
Will fetch all records by calling CoqModel.$resource.query()
Will perform a search using Coq.$conditionBuilder
Default CoqProvider.$conditionBuilder
implementation just pass find
argument to model resource parameters.
You can override CoqProvider.$conditionBuilder
in your application module config block
by requesting CoqProvider
as dependency.
Example, if you want conditions to be support finder query like :
// GET /api/user?conditions[0][key]=age&conditions[0][op]='<'&conditions[0][value]=10&...
MyModel.where({ age : { $lt : 10 }, name : { $like : 'charly' } });
app.config(function(CoqProvider) {
CoqProvider.$conditionBuilder = function(queryObject) {
var query = [];
angular.forEach(function(v, k) {
if (angular.isObject(v)) {
var item = {
key : k,
value : v
},
op = Object.keys(v)[0];
if (op == '$lt') {
item.op = '<';
} else if (op == '$gt') {
item.op = '>';
} else if (op == '$lte') {
item.op = '<=';
} else if (op == '$gte') {
item.op = '>=';
} else if (op == '$like') {
item.op = '%';
} else if (op == '$not') {
item.op = '!=';
} else {
return;
}
query.push(item);
} else {
query.push({
key : k,
op : '=',
value : v
});
}
});
return {
conditions : query
};
};
});