This repository has been archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clayallsopp/react.backbone#18 commit https://github.com/EamonNerbonne/react.backbone/commit/18742237d8a34cd5f77608c3a97d02728358df72
- Loading branch information
1 parent
4b5cf24
commit cadc6e3
Showing
1 changed file
with
85 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
})); |