Skip to content

Commit

Permalink
fix: model should not inject if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Oct 13, 2016
1 parent 58c27b6 commit caaaa97
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/createDva.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ export default function createDva(createOpts) {

// inject model dynamically
function injectModel(createReducer, onError, m) {
if (m.namespace) {
const hasExisted = this._models.some(model =>
model.namespace === m.namespace
);
if (hasExisted) {
return;
}
}
m = checkModel(m, mobile);
this._models.push(m);
const store = this._store;

// reducers
Expand Down
22 changes: 22 additions & 0 deletions test/app.model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,26 @@ describe('app.model', () => {
// effects
expect(count).toEqual(2);
});

it('don\'t inject if exists', () => {
const app = dva();
let count = 0;

const model = {
namespace: 'count',
state: 0,
subscriptions: {
setup() {
count += 1;
},
},
};

app.model(model);
app.router(() => 1);
app.start();
app.model(model);

expect(count).toEqual(1);
});
});

0 comments on commit caaaa97

Please sign in to comment.