Skip to content

Commit

Permalink
Merge 9b03471 into cad4742
Browse files Browse the repository at this point in the history
  • Loading branch information
krawaller committed Dec 17, 2014
2 parents cad4742 + 9b03471 commit 0404549
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/reactfire.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function staticMaker(method,firebaseRef,bindVar,cancelCallback){
return {
componentWillMount: function(){
this.firebaseRefs = this.firebaseRefs || {};
this.firebaseListeners = this.firebaseListeners || {};
var tosteal = ["bindAsArray","bindAsObject","_bind","_validateBindVar","unbind","_isArray","_toArray"];
for(var m=0; m < tosteal.length; m++){
this[tosteal[m]] = ReactFireMixin[tosteal[m]];
}
this[method](firebaseRef,bindVar,cancelCallback);
},
componentWillUnmount: ReactFireMixin.componentWillUnmount
};
}

var ReactFireMixin = {
/********************/
/* MIXIN LIFETIME */
Expand All @@ -23,12 +38,20 @@ var ReactFireMixin = {
/*************/
/* Creates a binding between Firebase and the inputted bind variable as an array */
bindAsArray: function(firebaseRef, bindVar, cancelCallback) {
this._bind(firebaseRef, bindVar, cancelCallback, true);
if (!this.setState){
return staticMaker("bindAsArray",firebaseRef,bindVar,cancelCallback);
} else {
this._bind(firebaseRef, bindVar, cancelCallback, true);
}
},

/* Creates a binding between Firebase and the inputted bind variable as an object */
bindAsObject: function(firebaseRef, bindVar, cancelCallback) {
this._bind(firebaseRef, bindVar, cancelCallback, false);
if (!this.setState){
return staticMaker("bindAsObject",firebaseRef,bindVar,cancelCallback);
} else {
this._bind(firebaseRef, bindVar, cancelCallback, false);
}
},

/* Creates a binding between Firebase and the inputted bind variable as either an array or object */
Expand Down Expand Up @@ -61,7 +84,7 @@ var ReactFireMixin = {
newState[bindVar] = dataSnapshot.val();
}
this.setState(newState);
}.bind(this), cancelCallback);
}.bind(this), this[cancelCallback] || cancelCallback);
},

/* Removes the binding between Firebase and the inputted bind variable */
Expand Down
42 changes: 42 additions & 0 deletions tests/specs/reactfire.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ describe("ReactFireMixin Tests:", function() {
React.render(new TestComponent(), document.body);
});

it("bindAsArray() static call binds to remote Firebase data as an array", function(done) {
var TestComponent = React.createClass({
mixins: [ReactFireMixin.bindAsArray(firebaseRef,"items")],

componentDidMount: function() {
firebaseRef.set({ a: 1, b: 2, c: 3 });
},

componentDidUpdate: function(prevProps, prevState) {
expect(this.state).toEqual({ items: [1, 2, 3] });
done();
},

render: function() {
return React.DOM.div(null, "Testing");
}
});

React.render(new TestComponent(), document.body);
});

it("bindAsArray() binds to remote Firebase data as an array (limit query)", function(done) {
var TestComponent = React.createClass({
mixins: [ReactFireMixin],
Expand Down Expand Up @@ -261,6 +282,27 @@ describe("ReactFireMixin Tests:", function() {
React.render(new TestComponent(), document.body);
});

it("bindAsObject() static binds to remote Firebase data as an object", function(done) {
var TestComponent = React.createClass({
mixins: [ReactFireMixin.bindAsObject(firebaseRef, "items")],

componentDidMount: function() {
firebaseRef.set({ a: 1, b: 2, c: 3 });
},

componentDidUpdate: function(prevProps, prevState) {
expect(this.state).toEqual({ items: { a: 1, b: 2, c: 3 } });
done();
},

render: function() {
return React.DOM.div(null, "Testing");
}
});

React.render(new TestComponent(), document.body);
});

it("bindAsObject() binds to remote Firebase data as an object (limit query)", function(done) {
var TestComponent = React.createClass({
mixins: [ReactFireMixin],
Expand Down

0 comments on commit 0404549

Please sign in to comment.