Skip to content

Commit 91fb4fb

Browse files
author
Erwan Carriou
committed
work in progress for the 0.6.0:
- add $behavior.get - add $workflow.action - update jsdoc
1 parent 7187eca commit 91fb4fb

File tree

4 files changed

+108
-34
lines changed

4 files changed

+108
-34
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "monoco",
33
"version": "0.6.0",
4-
"description": "A Model and a NoSQL Database for Component",
4+
"description": "A Model and a NoSQL Database for Components",
55
"license": "GPL-3.0",
66
"homepage": "https://github.com/monocojs/monoco.git",
77
"author": {

src/behavior.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ function clear() {
252252
}
253253

254254

255+
/*
256+
* Get a behavior by its id.
257+
* @method get
258+
* @param {String} id
259+
* @return {Behavior} the behavior
260+
*/
261+
function get(id) {
262+
return store[id];
263+
}
264+
265+
255266
/* exports */
256267

257268

@@ -282,6 +293,15 @@ function clear() {
282293
exports.add = add;
283294

284295

296+
/**
297+
* Get a behavior by its id.
298+
* @method get
299+
* @param {String} id
300+
* @return {Behavior} the behavior
301+
*/
302+
exports.get = get;
303+
304+
285305
/**
286306
* Remove a behavior with its id or remove all the behaviors for a specific state
287307
* of the component.

src/component.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,9 @@ function addEvents(model, Class, classId) {
588588
*/
589589
function addOn(Class, classId) {
590590
var body = function (state, handler, useCoreAPI) {
591-
var result = '';
591+
var behaviorId = '',
592+
currentState = '';
593+
592594
if ($workflow.checkParams({
593595
"component": this,
594596
"methodName": "on",
@@ -606,16 +608,11 @@ function addOn(Class, classId) {
606608
$log.behaviorNotUnique(classId, state);
607609
} else {
608610
if ($worklow.validParamNumbers(classId, state, handler)) {
609-
result = $behavior.add(this.id(), state, handler, useCoreAPI);
610-
611-
// experimental
612-
var currentState = $state.get(this.id());
611+
behaviorId = $behavior.add(this.id(), state, handler, useCoreAPI);
612+
613+
currentState = $state.get(this.id());
613614
if (currentState && state === currentState.name) {
614-
$workflow.state({
615-
"component": this.id(),
616-
"state": state,
617-
"data": currentState.parameters.data
618-
});
615+
$workflow.action(behaviorId, behaviorId);
619616
}
620617

621618
} else {
@@ -626,7 +623,7 @@ function addOn(Class, classId) {
626623
$log.invalidStateOn(classId, state);
627624
}
628625
}
629-
return result;
626+
return behaviorId;
630627
};
631628
/* jshint -W054 */
632629
Class.prototype.on = new Function("body", "return function on (state,handler,useCoreAPI) { return body.call(this,state,handler,useCoreAPI) };")(body);
@@ -643,7 +640,9 @@ function addOn(Class, classId) {
643640
*/
644641
function addOnClass(Class, classId) {
645642
var body = function (state, handler, useCoreAPI) {
646-
var result = '';
643+
var behaviorId = '',
644+
currentState = '';
645+
647646
if ($workflow.checkParams({
648647
"component": this,
649648
"methodName": "on",
@@ -661,16 +660,11 @@ function addOnClass(Class, classId) {
661660
$log.behaviorNotUnique(classId, state);
662661
} else {
663662
if ($worklow.validParamNumbers(classId, state, handler)) {
664-
result = $behavior.add(this.id(), state, handler, useCoreAPI);
665-
666-
// experimental
667-
var currentState = $state.get(this.id());
663+
behaviorId = $behavior.add(this.id(), state, handler, useCoreAPI);
664+
665+
currentState = $state.get(this.id());
668666
if (currentState && state === currentState.name) {
669-
$workflow.state({
670-
"component": this.id(),
671-
"state": state,
672-
"data": currentState.parameters.data
673-
});
667+
$workflow.action(behaviorId, behaviorId);
674668
}
675669

676670
} else {
@@ -681,7 +675,7 @@ function addOnClass(Class, classId) {
681675
$log.invalidStateOn(classId, state);
682676
}
683677
}
684-
return result;
678+
return behaviorId;
685679
};
686680
/* jshint -W054 */
687681
Class.on = new Function("body", "return function on (state,handler,useCoreAPI) { return body.call(this, state, handler, useCoreAPI) };")(body);
@@ -747,7 +741,6 @@ function addDestroyClass(Class) {
747741
}
748742

749743

750-
751744
/*
752745
* Add the addClassInfo method on a class.
753746
* @method addClassInfoClass
@@ -827,10 +820,16 @@ function get(id) {
827820

828821

829822
/*
830-
* Find components with a query.
823+
* Find components with a query.
831824
* @method find
832-
* @param {String} query query
825+
* @param {String} Class class name of the component to search
826+
* @param {Object|Array} query query
833827
* @return {Array} components found
828+
*
829+
* @example
830+
* $component.find("Student", {"name": "laure"}); <br>
831+
* $component.find("Student", {"name": "laure", "age" : 24}); <br>
832+
* $component.find("Student", [{"name": "rene"}, {"name": "robert"}]);
834833
*/
835834
function find(Class, query) {
836835
var documents = [],
@@ -959,8 +958,14 @@ exports.get = get;
959958
/**
960959
* Find components with a query.
961960
* @method find
962-
* @param {String} query query
961+
* @param {String} Class class name of the component to search
962+
* @param {Object|Array} query query
963963
* @return {Array} components found
964+
*
965+
* @example
966+
* $component.find("Student", {"name": "laure"}); <br>
967+
* $component.find("Student", {"name": "laure", "age" : 24}); <br>
968+
* $component.find("Student", [{"name": "rene"}, {"name": "robert"}]);
964969
*/
965970
exports.find = find;
966971

src/workflow.js

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ function getActions(component, name, isEvent) {
321321
* @method callAction
322322
* @param {Component} component
323323
* @param {String} state name of the state
324-
* @param {Array} action action
324+
* @param {Object} action action
325325
* @param {Array} params parameters of the action
326326
* @param {Boolean} isEvent is the action a callback of an event
327327
* @return {Boolean} result of the action
@@ -523,6 +523,47 @@ function checkParams(params) {
523523
}
524524

525525

526+
/*
527+
* Call an action that comes from an event.
528+
* @method action
529+
* @param {String} behaviorId id of the behavior
530+
* @param {Array} params parameters
531+
*/
532+
function action(behaviorId, params) {
533+
var isEvent = false,
534+
isProperty = false,
535+
isCollection = false,
536+
behaviors = [],
537+
behavior = null,
538+
component = null,
539+
actionFromMemory = null;
540+
541+
behaviors = $db.MonocoBehavior.find({
542+
"_id": behaviorId
543+
});
544+
545+
actionFromMemory = $behavior.get(behaviorId);
546+
547+
if (behaviors.length === 1) {
548+
behavior = behaviors[0];
549+
550+
component = $component.get(behavior.component);
551+
if (component) {
552+
isEvent = $metamodel.isEvent(behavior.state, component.constructor.name);
553+
isProperty = $metamodel.isProperty(behavior.state, component.constructor.name);
554+
isCollection = $metamodel.isCollection(behavior.state, component.constructor.name);
555+
556+
if (isEvent || isProperty || isCollection) {
557+
callAction(component, behavior.state, {
558+
"useCoreAPI": behavior.useCoreAPI,
559+
"action": actionFromMemory
560+
}, params, true);
561+
}
562+
}
563+
}
564+
}
565+
566+
526567
/*
527568
* Change the state of a component.
528569
*
@@ -590,13 +631,12 @@ function state(params) {
590631
action = actions[0];
591632
result = callAction(component, params.state, action, params.data, false);
592633

593-
if (checkResult({
634+
checkResult({
594635
"component": component,
595636
"methodName": params.state,
596637
"methodResult": result
597-
})) {
598-
// $state.set(component.id(), params.state, params.data);
599-
}
638+
});
639+
600640
} else {
601641

602642
length = actions.length;
@@ -610,7 +650,7 @@ function state(params) {
610650
}
611651
return result;
612652
} else {
613-
if (component && (isEvent || isProperty || isCollection)) {
653+
if (component && (isEvent || isProperty || isCollection)) {
614654
$state.set(component.id(), params.state, params.data);
615655
}
616656
}
@@ -743,4 +783,13 @@ exports.checkParams = checkParams;
743783
* @param {Function} action action
744784
* @return {Boolean} true if the action is the valid number of parameters
745785
*/
746-
exports.validParamNumbers = validParamNumbers;
786+
exports.validParamNumbers = validParamNumbers;
787+
788+
789+
/**
790+
* Call an action that comes from an event.
791+
* @method action
792+
* @param {String} behaviorId id of the behavior
793+
* @param {Array} params parameters
794+
*/
795+
exports.action = action;

0 commit comments

Comments
 (0)