mirror of
https://github.com/zhigang1992/reactfire.git
synced 2026-04-01 22:42:36 +08:00
Merge branch 'puf-add-id-to-array-items' of https://github.com/puf/reactfire into puf-puf-add-id-to-array-items
This commit is contained in:
@@ -120,12 +120,19 @@ var ReactFireMixin = {
|
||||
var out = [];
|
||||
if (obj) {
|
||||
if (this._isArray(obj)) {
|
||||
out = obj;
|
||||
for (var i=0; i < obj.length; i++) {
|
||||
out.push({ $id: i, $value: obj[i] });
|
||||
}
|
||||
}
|
||||
else if (typeof(obj) === "object") {
|
||||
for (var key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
out.push(obj[key]);
|
||||
var item = obj[key];
|
||||
if (typeof(item) !== "object") {
|
||||
item = { $value: item };
|
||||
}
|
||||
item.$id = key;
|
||||
out.push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ describe("ReactFireMixin Tests:", function() {
|
||||
},
|
||||
|
||||
componentDidUpdate: function(prevProps, prevState) {
|
||||
expect(this.state).toEqual({ items: [1, 2, 3] });
|
||||
expect(this.state).toEqual({ items: [{ $id: "a", $value: 1 }, { $id: "b", $value: 2 }, { $id: "c", $value: 3 }] });
|
||||
done();
|
||||
},
|
||||
|
||||
@@ -134,7 +134,7 @@ describe("ReactFireMixin Tests:", function() {
|
||||
},
|
||||
|
||||
componentDidUpdate: function(prevProps, prevState) {
|
||||
expect(this.state).toEqual({ items: [2, 3] });
|
||||
expect(this.state).toEqual({ items: [{ $id: "b", $value: 2 }, { $id: "c", $value: 3 }] });
|
||||
done();
|
||||
},
|
||||
|
||||
@@ -145,6 +145,48 @@ describe("ReactFireMixin Tests:", function() {
|
||||
|
||||
React.render(new TestComponent(), document.body);
|
||||
});
|
||||
|
||||
it("bindAsArray() makes $id available on array items when they are objects", function(done) {
|
||||
var TestComponent = React.createClass({
|
||||
mixins: [ReactFireMixin],
|
||||
componentWillMount: function() {
|
||||
this.bindAsArray(firebaseRef, "items");
|
||||
},
|
||||
componentDidMount: function() {
|
||||
firebaseRef.set({ first: { index: 1 }, second: { index: 2 }, third: { index: 3 } });
|
||||
},
|
||||
componentDidUpdate: function(prevProps, prevState) {
|
||||
expect(this.state.items.map(function(item) { return item.$id; })).toEqual(["first", "second", "third"]);
|
||||
done();
|
||||
},
|
||||
render: function() {
|
||||
return React.DOM.div(null, "Testing");
|
||||
}
|
||||
});
|
||||
React.render(new TestComponent(), document.body);
|
||||
})
|
||||
|
||||
it("bindAsArray() makes $id available on array items when they are primitives", function(done) {
|
||||
var TestComponent = React.createClass({
|
||||
mixins: [ReactFireMixin],
|
||||
componentWillMount: function() {
|
||||
this.bindAsArray(firebaseRef, "items");
|
||||
},
|
||||
componentDidMount: function() {
|
||||
firebaseRef.set(["first", "second", "third"]);
|
||||
},
|
||||
componentDidUpdate: function(prevProps, prevState) {
|
||||
console.log(JSON.stringify(this.state));
|
||||
expect(this.state.items.map(function(item) { return item.$id; })).toEqual([0, 1, 2]);
|
||||
expect(this.state.items.map(function(item) { return item.$value; })).toEqual(["first", "second", "third"]);
|
||||
done();
|
||||
},
|
||||
render: function() {
|
||||
return React.DOM.div(null, "Testing");
|
||||
}
|
||||
});
|
||||
React.render(new TestComponent(), document.body);
|
||||
})
|
||||
});
|
||||
|
||||
describe("bindAsObject():", function() {
|
||||
|
||||
Reference in New Issue
Block a user