Skip to content

Commit

Permalink
Add a setState and emitChange method
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Mar 5, 2015
1 parent 1d62528 commit 6e45ae4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
12 changes: 11 additions & 1 deletion dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,18 @@ var Alt = (function () {
_storeName: key,
alt: this,
dispatcher: this.dispatcher,
getInstance: function () {
getInstance: function getInstance() {
return storeInstance;
},
emitChange: function emitChange() {
this.getInstance().emitChange();
},
setState: function setState() {
var values = arguments[0] === undefined ? {} : arguments[0];

assign(this, values);
this.emitChange();
return false;
}
});

Expand Down
12 changes: 11 additions & 1 deletion dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,18 @@ var Alt = (function () {
_storeName: key,
alt: this,
dispatcher: this.dispatcher,
getInstance: function () {
getInstance: function getInstance() {
return storeInstance;
},
emitChange: function emitChange() {
this.getInstance().emitChange();
},
setState: function setState() {
var values = arguments[0] === undefined ? {} : arguments[0];

assign(this, values);
this.emitChange();
return false;
}
});

Expand Down
12 changes: 11 additions & 1 deletion src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,17 @@ your own custom identifier for each store`
_storeName: key,
alt: this,
dispatcher: this.dispatcher,
getInstance: () => storeInstance
getInstance() {
return storeInstance
},
emitChange() {
this.getInstance().emitChange()
},
setState(values = {}) {
assign(this, values)
this.emitChange()
return false
}
})

const store = new Store(this)
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ let tests = {
onTest() {
setTimeout(() => {
this.test = true
this.getInstance().emitChange()
this.emitChange()
})
return false
}
Expand Down
46 changes: 46 additions & 0 deletions test/setting-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import assert from 'assert'
import Alt from '../dist/alt-with-runtime'

let alt = new Alt()

let actions = alt.generateActions('fire', 'nothing')

class MyStore {
constructor() {
this.foo = 1
this.bindListeners({ increment: actions.FIRE, nothing: actions.NOTHING })
}

increment() {
this.retVal = this.setState({ foo: this.foo + 1 })
return this.retVal
}

nothing() {
this.setState()
}
}

let myStore = alt.createStore(MyStore)

export default {
'using setState to set the state'() {
actions.fire()

let changes = 0
let fart = () => {
changes += 1
assert.equal(changes, 1, 'listen was fired once')
}

myStore.listen(fart)

assert.equal(myStore.getState().foo, 2, 'foo was incremented')
assert.equal(myStore.getState().retVal, false, 'return value of setState is false')

myStore.unlisten(fart)

// calling set state without anything doesn't make things crash and burn
actions.nothing()
}
}

0 comments on commit 6e45ae4

Please sign in to comment.