Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
Update react.backbone
Browse files Browse the repository at this point in the history
  • Loading branch information
EamonNerbonne committed Apr 16, 2014
1 parent 4b5cf24 commit cadc6e3
Showing 1 changed file with 85 additions and 84 deletions.
169 changes: 85 additions & 84 deletions core/vendor/assets/javascripts/react.backbone.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,101 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['backbone', 'react'], factory);
} else {
// Browser globals
root.amdWeb = factory(root.Backbone, root.React);
}
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['backbone', 'react'], factory);
} else {
// Browser globals
root.amdWeb = factory(root.Backbone, root.React);
}
}(this, function (Backbone, React) {
"use strict";

var collection_behavior = {
change_options: 'add remove reset sort',
update_scheduler: function(func){ return _.debounce(func,0); }
};
var model_behavior = {
change_options: 'change',
update_scheduler: _.identity
//note: if we debounce models too we can no longer use model attributes
//as properties to react controlled components due to https://github.com/facebook/react/issues/955
};
"use strict";

var subscribe = function(component, model, customChangeOptions) {
if (!model) {
return;
}
var collection_behavior = {
changeOptions: 'add remove reset sort',
update_scheduler: function(func) { return _.debounce(func, 0); }
};
var model_behavior = {
changeOptions: 'change',
update_scheduler: _.identity
//note: if we debounce models too we can no longer use model attributes
//as properties to react controlled components due to https://github.com/facebook/react/issues/955
};

var behavior = model instanceof Backbone.Collection ? collection_behavior : model_behavior;
var subscribe = function(component, model, customChangeOptions) {
if (!model) {
return;
}

var triggerUpdate = behavior.update_scheduler(function() {
if (component.isMounted())
(component.onModelChange || component.forceUpdate).call(component);
});
var behavior = model instanceof Backbone.Collection ? collection_behavior : model_behavior;

model.on(customChangeOptions || behavior.change_options, triggerUpdate, component);
};
var triggerUpdate = behavior.update_scheduler(function() {
if (component.isMounted())
(component.onModelChange || component.forceUpdate).call(component);
});
var changeOptions = customChangeOptions || component.changeOptions || behavior.changeOptions;
model.on(changeOptions, triggerUpdate, component);
};

var unsubscribe = function(component, model) {
if (!model) {
return;
}
model.off(null, null, component);
};
React.BackboneMixin = function(prop_name, customChangeOptions){ return {
componentDidMount: function() {
// Whenever there may be a change in the Backbone data, trigger a reconcile.
subscribe(this, this.props[prop_name], customChangeOptions);
},
componentWillReceiveProps: function(nextProps) {
if (this.props[prop_name] === nextProps[prop_name]) {
return;
}
var unsubscribe = function(component, model) {
if (!model) {
return;
}
model.off(null, null, component);
};
React.BackboneMixin = function(prop_name, customChangeOptions) { return {
componentDidMount: function() {
// Whenever there may be a change in the Backbone data, trigger a reconcile.
subscribe(this, this.props[prop_name], customChangeOptions);
},
componentWillReceiveProps: function(nextProps) {
if (this.props[prop_name] === nextProps[prop_name]) {
return;
}

unsubscribe(this, this.props[prop_name]);
subscribe(this, nextProps[prop_name], customChangeOptions);
unsubscribe(this, this.props[prop_name]);
subscribe(this, nextProps[prop_name], customChangeOptions);

if (typeof this.componentWillChangeModel === 'function') {
this.componentWillChangeModel();
}
},
componentDidUpdate: function(prevProps, prevState) {
if (this.props[prop_name] === prevProps[prop_name]) {
return;
}
if (typeof this.componentWillChangeModel === 'function') {
this.componentWillChangeModel();
}
},
componentDidUpdate: function(prevProps, prevState) {
if (this.props[prop_name] === prevProps[prop_name]) {
return;
}

if (typeof this.componentDidChangeModel === 'function') {
this.componentDidChangeModel();
}
},
componentWillUnmount: function() {
// Ensure that we clean up any dangling references when the component is destroyed.
unsubscribe(this, this.props[prop_name]);
}
};};
if (typeof this.componentDidChangeModel === 'function') {
this.componentDidChangeModel();
}
},
componentWillUnmount: function() {
// Ensure that we clean up any dangling references when the component is destroyed.
unsubscribe(this, this.props[prop_name]);
}
};};
_.extend(React.BackboneMixin, React.BackboneMixin('model'));

React.BackboneViewMixin = {
getModel: function() {
return this.props.model;
},
model: function() {
return this.getModel();
},
el: function() {
return this.isMounted() && this.getDOMNode();
}
};
React.BackboneViewMixin = {
getModel: function() {
return this.props.model;
},
model: function() {
return this.getModel();
},
el: function() {
return this.isMounted() && this.getDOMNode();
}
};

React.createBackboneClass = function(spec) {
var currentMixins = spec.mixins || [];
React.createBackboneClass = function(spec) {
var currentMixins = spec.mixins || [];

spec.mixins = currentMixins.concat([
React.BackboneMixin('model', spec.changeOptions),
React.BackboneViewMixin
]);
return React.createClass(spec);
};
spec.mixins = currentMixins.concat([
React.BackboneMixin('model'),
React.BackboneViewMixin
]);
return React.createClass(spec);
};

return React;
return React;

}));

0 comments on commit cadc6e3

Please sign in to comment.