diff --git a/app/assets/javascripts/discourse/models/rest.js b/app/assets/javascripts/discourse/models/rest.js index 40b38e5107b..e0a697e8275 100644 --- a/app/assets/javascripts/discourse/models/rest.js +++ b/app/assets/javascripts/discourse/models/rest.js @@ -9,6 +9,9 @@ const RestModel = EmberObject.extend({ isSaving: false, beforeCreate() {}, + afterCreate() {}, + + beforeUpdate() {}, afterUpdate() {}, update(props) { @@ -18,6 +21,8 @@ const RestModel = EmberObject.extend({ props = props || this.updateProperties(); + this.beforeUpdate(props); + this.set("isSaving", true); return this.store .update(this.__type, this.id, props) @@ -65,6 +70,7 @@ const RestModel = EmberObject.extend({ this.set("__state", "created"); } + this.afterCreate(res); res.target = this; return res; }) diff --git a/test/javascripts/models/rest-model-test.js.es6 b/test/javascripts/models/rest-model-test.js.es6 index c7c44bba269..8213e96e8d6 100644 --- a/test/javascripts/models/rest-model-test.js.es6 +++ b/test/javascripts/models/rest-model-test.js.es6 @@ -24,10 +24,14 @@ QUnit.test("update", async assert => { assert.equal(widget.get("name"), "Trout Lure"); assert.ok(!widget.get("isSaving"), "it is not saving"); + const spyBeforeUpdate = sandbox.spy(widget, "beforeUpdate"); + const spyAfterUpdate = sandbox.spy(widget, "afterUpdate"); const promise = widget.update({ name: "new name" }); assert.ok(widget.get("isSaving"), "it is saving"); + assert.ok(spyBeforeUpdate.calledOn(widget)); const result = await promise; + assert.ok(spyAfterUpdate.calledOn(widget)); assert.ok(!widget.get("isSaving"), "it is no longer saving"); assert.equal(widget.get("name"), "new name"); @@ -61,10 +65,14 @@ QUnit.test("save new", async assert => { assert.ok(!widget.get("isCreated"), "it is not created"); assert.ok(!widget.get("isSaving"), "it is not saving"); + const spyBeforeCreate = sandbox.spy(widget, "beforeCreate"); + const spyAfterCreate = sandbox.spy(widget, "afterCreate"); const promise = widget.save({ name: "Evil Widget" }); assert.ok(widget.get("isSaving"), "it is not saving"); + assert.ok(spyBeforeCreate.calledOn(widget)); const result = await promise; + assert.ok(spyAfterCreate.calledOn(widget)); assert.ok(!widget.get("isSaving"), "it is no longer saving"); assert.ok(widget.get("id"), "it has an id"); assert.ok(widget.get("name"), "Evil Widget");