Skip to content

Commit

Permalink
Merge pull request #30 from isabo/simplified
Browse files Browse the repository at this point in the history
Remove trigger functionality
  • Loading branch information
isabo committed Dec 6, 2015
2 parents 1e9699d + d0a3aab commit 3ab7580
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 361 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ Already implemented:
* Makes **Promises**, instead of actually calling you back. :laughing:

Coming soon:
* Loose coupling between locally triggered "chain-reaction" operations, allowing more
compartmentalization and less spaghetti code. The most common use-case for this is for building
indexes that take care of themselves.
* Atomic operations that involve multiple models simultaneously.
* **Persistence!** This is for loading data while offline, or for priming your app for a quick start.

Here are [more details](https://github.com/isabo/onfire/wiki/OnFire-Goals-&-Requirements).
Expand Down Expand Up @@ -60,7 +58,7 @@ person.
firstName('Fred').
lastName('Bloggs').
save().
then(...);
then(...);

// This will throw an exception, because of the mis-spelt property name.
// Schema-driven modules reduce the chances of unintentional errors in your code.
Expand Down
18 changes: 9 additions & 9 deletions dist/onfire-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,6 @@ onfire.model.Model.prototype.dispose = function() {};
onfire.model.Model.prototype.exists = function() {};


/**
* Register the callback function that will be called whenever the model is updated. To deregister
* an existing callback, just pass null as the callback argument.
*
* @param {function()|null} callback
*/
onfire.model.Model.prototype.onValueChanged = function(callback) {};


/**
* Synchronously retrieves the value associated with a key. If the value is not a primitive, a model
* instance will be returned, in which case .whenLoaded() should be called on the returned model in
Expand Down Expand Up @@ -525,6 +516,15 @@ onfire.model.Model.prototype.hasChanges = function() {};
onfire.model.Model.prototype.key = function() {};


/**
* Register the callback function that will be called whenever the model is updated. To deregister
* an existing callback, just pass null as the callback argument.
*
* @param {function()|null} callback
*/
onfire.model.Model.prototype.onValueChanged = function(callback) {};


/**
* Asynchronously commits the outstanding changes.
*
Expand Down
99 changes: 47 additions & 52 deletions dist/onfire.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/onfire.min.js.map

Large diffs are not rendered by default.

36 changes: 8 additions & 28 deletions src/model/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ goog.provide('onfire.model.Collection');

goog.require('onfire.model.Model');
goog.require('onfire.model.Error');
goog.require('onfire.triggers');
goog.require('onfire.utils.firebase.EventType');
goog.require('onfire.utils.promise');
goog.require('goog.object');
Expand Down Expand Up @@ -214,7 +213,7 @@ onfire.model.Collection.prototype.set = function(key, value) {

/**
* @override the return type.
* @return {!onfire.model.Collection}
* @return {!Promise<!onfire.model.Collection,!Error>|!goog.Promise<!onfire.model.Collection,!Error>}
*/
onfire.model.Collection.prototype.save;

Expand All @@ -223,7 +222,7 @@ onfire.model.Collection.prototype.save;
* @override the return type.
* @param {!Object<string,Firebase.Value>} pairs An object containing the property/value pairs to
* update.
* @return {!onfire.model.Collection}
* @return {!Promise<!onfire.model.Collection,!Error>|!goog.Promise<!onfire.model.Collection,!Error>}
* @protected
*/
onfire.model.Collection.prototype.update;
Expand Down Expand Up @@ -340,31 +339,12 @@ onfire.model.Collection.prototype.remove = function(key) {
return onfire.utils.promise.resolve(null);
}

// We need an instance of the item being removed, so that we can provide it to the trigger
// function. Nope -- by the time that happens, its properties will be null?!! Add a .disconnect()
// method that will freeze the current snapshot?
var promise = this.memberCtor_ ?
this.fetch(key) : onfire.utils.promise.resolve(this.getBasicValue(key));

var removed;
var self = this;
return promise.
then(function(/** !onfire.model.Model|Firebase.Value */item) {
removed = item;
}).
then(function() {
// self.set(key, null); <-- will throw an exception if collection members are models.
self.changes[key] = null;
self.hasOustandingChanges = true;
return self.save();
}).
then(function() {
return onfire.triggers.triggerChildRemoved(self.ref, removed, key);
}).
then(function() {
if (removed instanceof onfire.model.Model) {
removed.dispose();
}
// self.set(key, null); <-- will throw an exception if collection members are models.
this.changes[key] = null;
this.hasOustandingChanges = true;
return this.save().
then(function(self) {
return null;
});
};

Expand Down
36 changes: 2 additions & 34 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ goog.require('onfire.model.schema');
goog.require('onfire.utils.firebase.EventType');
goog.require('onfire.utils.promise');
goog.require('onfire.utils.logging');
goog.require('onfire.triggers');
goog.require('goog.object');


Expand Down Expand Up @@ -198,7 +197,6 @@ onfire.model.Model.prototype.configureInstance = function(schema) {
};



/**
* Load the initial data and watch for changes.
*
Expand Down Expand Up @@ -520,43 +518,18 @@ onfire.model.Model.prototype.save = function() {
onfire.model.Model.prototype.update = function(pairs) {

// TODO: validate.
// Remember the old values for comparison afterwards.
var oldCount = this.childrenCount;
var oldPairs = {};
for (var p in pairs) {
try {
oldPairs[p] = this.getBasicValue(p);
} catch (e) {
// Ignore 'No such property' errors. What they mean is 'No such property ... yet'
}
}

var oldCount = this.childrenCount;
var self = this;
return this.ref.update(pairs).
then(function() {
var promises = [];
for(p in pairs) {
if (oldPairs[p] !== pairs[p]) {
var valueRef = self.ref.child(p);
var pr = onfire.triggers.triggerValueChanged(valueRef, oldPairs[p],
pairs[p]);
promises.push(pr);
}
}
return onfire.utils.promise.all(promises);
}).
then(function() {
if (oldCount > 0 && self.childrenCount === 0) {
// This object just disappeared because we set its remaining properties to null.
onfire.utils.logging.info('REMOVED ' + self.ref.path());
return onfire.triggers.triggerChildRemoved(self.ref.parent(), self, self.key());
} else if (oldCount === 0 && self.childrenCount > 0) {
// This object just came back into existence.
onfire.utils.logging.info('CREATED ' + self.ref.path());
return onfire.triggers.triggerChildAdded(self.ref.parent(), self);
}
}).
then(function() {
return self;
});
};
Expand Down Expand Up @@ -584,13 +557,8 @@ onfire.model.Model.prototype.initializeValues = function(values) {
then(function(/** !Object */result) {
if (result['isCommitted']) {
onfire.utils.logging.info('CREATED ' + self.ref.path());
return onfire.triggers.triggerChildAdded(self.ref.parent(), self).
then(function() {
return result;
});
} else {
return result;
}
return result;
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ onfire.Ref.prototype.key = function() {
*/
onfire.Ref.prototype.off = function(opt_eventType, opt_callback, opt_context) {

this.ref_.off(opt_eventType, opt_callback, opt_context);
this.ref_.off(opt_eventType, opt_callback); // Because we wrap all callbacks, there is no context.
};


Expand Down
Loading

0 comments on commit 3ab7580

Please sign in to comment.