README
jquery-statebus
π Small state management library for jQuery
Example
var counter = $.statebus('counter', {
state: {
value: 0
},
action: {
increment: function(number){
return {value: this.state.value + number}
},
decrement: function(number){
return {value: this.state.value - number}
}
}
})
var $el = $('.counter')
$el.on('click', '[data-counter="increment"]', function(counter){
counter.action.increment(1)
})
$el.on('click', '[data-counter="decrement"]', function(counter){
counter.action.decrement(1)
})
counter.on('increment decrement', function(counter){
$el.find('.txt').text(counter.state.value)
})
What?
jquery-statebusλ View(jqueryλ‘ μμ±λ)μμ μνλ₯Ό λΆλ¦¬νλ μμ£Ό κ°λ¨ν ν¨ν΄μ μ 곡ν©λλ€.
- λμμΈμ΄ λ°λ λ μλ°μ€ν¬λ¦½νΈκ° λ§κ°μ§λ κ²μ μ΅μνν©λλ€.
- νμ₯μ΄ νΈν©λλ€.
- backbone λ³΄λ€ λ μ½μ΅λλ€.
- μκ³ κ°λ³μ΅λλ€.
Install
yarn install jquery-statebus
// index.js
require('jquery-statebus');
browser
<script src="https://unpkg.com/jquery-statebus"></script>
How to use
State
λ€μμ€νμ΄μ€λ‘ stateλ₯Ό μ μν©λλ€.
var counter = $.statebus('counter', { // namespaceλ 'counter'κ° λ©λλ€.
state: { value: 1 }
})
// counter.state.value == 1
// $.statebus.state.counter.value == 1
$.statebus.state[namespace]
λ‘ λ€λ₯Έ μ§μstateλ₯Ό κ°μ Έμ¬ μ μμ΅λλ€.
Action
var counter = $.statebus('counter', {
state: { value: 1 },
action:{
increment: function(number){
return {value: this.state.value + number}
}
}
})
counter.action.increment(1) // "counter.state.value" to be 2
$.statebus.action.counter.increment(2) // "counter.state.value" to be 4
actionμ λ°νκ²°κ³Όλ‘ μνλ₯Ό λ°κΏλλ€. ($.extend
ν¨μλ₯Ό μ¬μ©ν©λλ€.)
action ν¨μμμ this
λ statebus κ°μ²΄μ λμΌν©λλ€.
Action in action
$.statebus('counter', {
state: { value: 1 },
action:{
increment: function(number){
return {value: this.state.value + number}
},
delayIncrement: function(number, sec){
setTimeout(this.action.increment, sec * 1000, number)
}
}
})
counter.action.delayIncrement(1, 3)
console.log( counter.state.value ) // 1
// ..after 3sec.
console.log( counter.state.value ) // 2
On(action, render [, immediately])
counter.on('increment', function(counter){
if(counter.state.value !== counter.prevState.value){
$display.text(counter.state.value)
}
})
jquery.statebusλ λ§λ²μ΄ μμ΅λλ€. μ§μ Viewμ μ°κ΄λ Actionμ ꡬλ νκ³ μ΄μ μνμ λΉκ΅ν΄μΌ ν©λλ€.
Unsubscribe
var unsubscribe = counter.on('increment', function(){ ... })
unsubscribe()
onλ©μλλ ꡬλ μ μ·¨μνλ ν¨μλ₯Ό λ°νν©λλ€. μνλ μμ μ ꡬλ μ μ·¨μν μ μμ΅λλ€.
Arguments
counter.on('increment', function(counter, ctx){
var amount = ctx.args[0]
...
})
νμνλ€λ©΄ μ‘μ μ μΈμλ₯Ό μ»μ μλ μμ΅λλ€.
Multiple subscription
// using space
counter.on('increment decrement', view)
// using array
counter.on(['increment', 'decrement'], view)
κ°μ View λ³κ²½μ 곡μ νλ Actionλ€μ μΈμ λ μ‘΄μ¬ν©λλ€.
space
, λλ array
λ‘ νλ²μ μ¬λ¬ Actionμ λν΄μ ꡬλ
ν μ μμ΅λλ€.
Global subscription
$.statebus.on(['counter.increment', 'other.update'], view)
λ€μμ€νμ΄μ€λ₯Ό μ¬μ©ν΄μ μλ‘ λ€λ₯Έ μ§μ μνμ λν λ³κ²½μ κ°μ 리μ€λλ‘ κ΅¬λ ν μ μμ΅λλ€.
Immediately
counter.on('increment', function (counter, ctx){
if(ctx.immediately) initView()
$display.text(counter.state.value)
}, true)
3λ²μ§Έ μΈμκ° trueλ©΄ ν¨μλ₯Ό μ¦μ μ€νν©λλ€. ctx.immediatelyλ‘ μ΄κΈ°μ€νμΈμ§ νλ¨ν μ μμ΅λλ€.
Override
$.statebus('test', {
state: { v1: 1 }
})
var re = $.statebus('test', {
state: { v2: 2 }
})
console.log( re.state ) // {v1: 1, v2: 2}
λ€μ μ¬μ μλλ©΄ μ΄μ μ μλ₯Ό μ μ§νλ©° νμ₯ν©λλ€.
$.statebus('test', {
state: { v1: 1 }
})
var re = $.statebus('test', {
state: { v2: 2 }
}, true) // Look here.
console.log( re.state ) // {v2: 2}
μ€λ²λΌμ΄λ μ΅μ μ΄ trueλ©΄, μ΄μ μ μ(μν, μ‘μ , μ΄λ²€νΈ 리μ€λ) λͺ¨λλ₯Ό μ§μ°κ³ μλ‘ μ μν©λλ€.
Remove
// μμ±
$.statebus('counter', { ... })
// μ κ±°
$.statebus.remove('counter')
remove λ©μλλ₯Ό μ¬μ©ν΄ μμ±λ μν, μ‘μ μ μ κ±°ν μ μμ΅λλ€.
Why?
jquery-statebusλ Viewμ Stateλ₯Ό λΆλ¦¬νλ κ² λͺ©μ μ λλ€. μλλ Viewμ Stateκ° κ°νκ² κ²°ν©λ μ½λμ λλ€.
$('#counter > button.increment').click(function(){
var $display = $('#counter span.display')
var number = parseInt($display.text())
$display.text(number + 1)
})
Viewμμ Stateλ₯Ό μ»μ΅λλ€.
μ΄κ²μ λννν κ²μ λλ€.
κΈ°λ₯μ΄ λμ΄λ μλ‘ λ³΅μ‘ν λ€νΈμν¬λ₯Ό λ§λλλ€. λμμΈλ³κ²½, κΈ°λ₯μΆκ°κ° νλ€μ΄μ§λλ€.
jquery-statebusλ‘ Stateμ Viewλ₯Ό λΆλ¦¬νλ©΄ μ΄λ¬ν 볡μ‘μ±μ κ°μ ν μ μμ΅λλ€.
Stateλ₯Ό λ©λͺ¨λ¦¬μ μλ λ 립λ κ°μ²΄μμ μ»κΈ° λλ¬Έμ λμμΈ λ³κ²½μΌλ‘ λ€λ₯Έ μλ°μ€ν¬λ¦½νΈ μ½λκ° λ§κ°μ§λ μΌμ μ΅μνν μ μμ΅λλ€. μλ‘μ΄ κΈ°λ₯μ μΆκ°ν λλ View μν κΉμ§ νμ ν΄μΌ νλ λΆλ΄μ΄ μ€μ΄λλλ€.
Tip
Use data attribute.
// bad
$el.find('button.increment').click(function(){
var amount = $(this).data('amount');
counter.action.increment(amount)
})
counter.on('increment', function(counter){
$el.find('span.display').text(counter.state.value)
})
//good
$el.on('click', '[data-counter="counter"]', function(counter){
var amount = $(this).data('params');
counter.action.increment(amount)
})
counter.on('increment', function(counter){
$el.find('[data-counter="value"]').text(counter.state.value)
})
λ°μ΄ν° μμ±μ View ꡬ쑰μ ꡬμλμ§ μμ΅λλ€. View λ³κ²½μ λΉκ΅μ μμ λ‘μ΄ μμ±μ λλ€. λ html μ½λλ§μΌλ‘ μ΄λ²€νΈκ° λ°μΈλ©λ¨μ μλ €μ£Όλ μν λ ν©λλ€.
jquery-statebusking
jquery-statebusking - statebusλ₯Ό backboneμ²λΌ μ¬μ©ν μ μμ΅λλ€.
License
MIT