Repository: dvajs/dva (js)

Issue:
routerRedux.push后会多次触发subscriptions
routerRedux.push后会多次触发subscriptions, 导致subscriptions里面的request会被多次执行

Pull request description:
fix: model should not inject if exists
Close #152 

Patch:
diff --git a/src/createDva.js b/src/createDva.js
index c1972ad..dcaadc6 100644
--- a/src/createDva.js
+++ b/src/createDva.js
@@ -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


Test patch:
diff --git a/test/app.model-test.js b/test/app.model-test.js
index 6667476..3ce81c7 100644
--- a/test/app.model-test.js
+++ b/test/app.model-test.js
@@ -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);
+  });
 });