Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional using a validator function #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions observable-slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ var ObservableSlim = (function() {
* change on any nested/child objects.
* @returns {ProxyConstructor} Proxy of the target object.
*/
var _create = function(target, domDelay, originalObservable, originalPath) {
var _create = function(target, domDelay, originalObservable, originalPath, validator_fn) {

var observable = originalObservable || null;
var validator = validator_fn || null;

// record the nested path taken to access this object -- if there was no path then we provide the first empty entry
var path = originalPath || [{"target":target,"property":""}];
Expand Down Expand Up @@ -205,7 +206,7 @@ var ObservableSlim = (function() {
// create a shallow copy of the path array -- if we didn't create a shallow copy then all nested objects would share the same path array and the path wouldn't be accurate
var newPath = path.slice(0);
newPath.push({"target":targetProp,"property":property});
return _create(targetProp, domDelay, observable, newPath);
return _create(targetProp, domDelay, observable, newPath, validator);
} else {
return targetProp;
}
Expand Down Expand Up @@ -311,6 +312,13 @@ var ObservableSlim = (function() {
,"proxy":proxy
});

if(validator != null && validator instanceof Function){
if(!validator(changes)){
changes = [];
return;
}
}

// mutations of arrays via .push or .splice actually modify the .length before the set handler is invoked
// so in order to accurately report the correct previousValue for the .length, we have to use a helper property.
if (property === "length" && target instanceof Array && target.__length !== value) {
Expand Down Expand Up @@ -550,7 +558,7 @@ var ObservableSlim = (function() {
* When invoked, this function is passed a single argument: an array of `ObservableSlimChange` detailing each change that has been made.
* @returns {ProxyConstructor} Proxy of the target object.
*/
create: function(target, domDelay, observer) {
create: function(target, domDelay, observer, validator) {

// test if the target is a Proxy, if it is then we need to retrieve the original object behind the Proxy.
// we do not allow creating proxies of proxies because -- given the recursive design of ObservableSlim -- it would lead to sharp increases in memory usage
Expand All @@ -562,7 +570,7 @@ var ObservableSlim = (function() {
}

// fire off the _create() method -- it will create a new observable and proxy and return the proxy
var proxy = _create(target, domDelay);
var proxy = _create(target, domDelay, null, null, validator);

// assign the observer function
if (typeof observer === "function") this.observe(proxy, observer);
Expand Down