ºê¹ÛÉÏ£¬Î¢ÐÅС³ÌÐòÊÇÓÉÒ»¸ö¸ö
Page ×é³ÉµÄ¡£ÓÐʱºòÎÒÃÇ»áÓöµ½Ò»Ð©ÒµÎñ´æÔÚñîºÏµÄ Page£¬Ò»¸ö Page Àïij¸ö״̬¸Ä±äºó£¬Ïà¹Ø
Page µÄ״̬ÐèÒª½øÐиüС£¶øÔÚС³ÌÐòÀÿ¸ö Page ¶¼ÊÇÒ»¸öÄ£¿é£¬ÓÐ×ŶÀÁ¢µÄ×÷ÓÃÓò£¬Òò´Ë Page
¼äÐèÒªÓÐÒ»ÖÖͨÐŲßÂÔ¡£
ÏëÏóÒ»¸öÒµÎñ³¡¾°£¬Óû§Ê×ÏȽøÈë¶©µ¥ÁбíÒ³¡£È»ºóµã»÷ÆäÖÐÒ»¸ö¶©µ¥£¬½øÈëµ½¶©µ¥ÏêÇéÒ³¡£µ±Óû§ÔÚ¶©µ¥ÏêÇéÒ³¶Ô¶©µ¥½øÐвÙ×÷£¬ÀýÈçÖ§¸¶¡¢È·ÈÏÊÕ»õµÈʱ£¬¸Ã¶©µ¥µÄ״̬¾Í»á·¢Éú¸Ä±ä¡£´ËʱÐèÒª¶ÔÉÏÒ»¼¶µÄ¶©µ¥ÁбíÒ³Öиö©µ¥µÄ״̬½øÐиüУº 
ÒªÏë¸üж©µ¥ÁбíÒ³µÄÊÓͼ²ã£¬¾ÍÐèÒªµ÷Óøà Page ¶ÔÏóµÄ setData ·½·¨¡£ÕâÀïΪ´ó¼ÒÁоÙÈýÖֱȽϳ£Óõķ½°¸£º
ÉèÖñê־λ
×î¼òµ¥µÄ·½·¨£¬ÔÚ¶©µ¥ÏêÇéÒ³¶Ô¶©µ¥µÄ²Ù×÷³É¹¦»Øµ÷ÖУ¬°ÑһЩ±ê־λÉèÖÃΪ true£¬²¢ÉèÖúòÎÊý£¨±ê־λºÍ²ÎÊý¿ÉÒÔ´æÔÚ
localStorage »ò¹ÒÔÚÈ«¾Ö App ¶ÔÏóÏ£©¡£È»ºóÿ´ÎÔÚ¶©µ¥ÁбíÒ³µÄ onShow ÉúÃüÖÜÆÚÖУ¬¸ù¾ÝÕâЩ±ê־λȥÅжÏÊÇ·ñ½øÐиüС¢¸üеIJÎÊýÊÇʲô¡£
ÕâÖÖ´¦ÀíÔÚÒµÎñÂß¼±È½Ï¼òµ¥¡¢Ò³Ãæ¼äµÄñîºÏ¶ÈºÜСʱ»¹Äܴպϣ¬Ò»µ©Âß¼¸´ÔÓÆðÀ´£¬¾ÍÐèҪдºÜ¶àÈßÓàµÄ´úÂ룬²¢ÇÒά»¤³É±¾»á·Ç³£¸ß¡£
Á÷³Ìͼ£º

ÀûÓÃÒ³ÃæÕ»»ñÈ¡ Page ¶ÔÏó
Èç¹û¶©µ¥ÏêÇéÒ³ÀïÄÜÄõ½¶©µ¥ÁбíÒ³µÄ Page ¶ÔÏ󣬾ÍÄÜÈ¥µ÷ÓÃËüµÄ setData ·½·¨¡£Ð¡³ÌÐòÌṩÁËÒ»¸ö·½·¨
getCurrentPages £¬Ö´ÐÐËü¿ÉÒԵõ½µ±Ç°Ò³ÃæÕ»µÄʵÀý£¬È»ºóÔÙ¸ù¾ÝÒ³Ãæ½øÕ»µÄ˳ÐòÎÒÃǾÍÄÜÄõ½¶©µ¥ÁбíÒ³µÄ
Page ¶ÔÏó¡£
È»¶øÕâÖÖ×ö·¨µÄȱµã»¹ÊÇñîºÏ¶ÈÌ«´ó£¬¹ý¶ÈÒÀÀµÒ³Ãæ½øÕ»Ë³Ðò¡£Ò»µ©ÔÚÒÔºóµÄ²úÆ·µü´úÖÐÒ³ÃæË³Ðò·¢Éú±ä»¯£¬½«ºÜÄÑȥά»¤¡£
Á÷³Ìͼ£º

ÉÏÊöÁ½ÖÖ·½·¨¶¼´æÔÚ×ÅñîºÏ¶È´ó¡¢Î¬»¤À§ÄѵÄÎÊÌ⣬¶øÀûÓ÷¢²¼£¯¶©ÔÄģʽÄܺܺõÄʵÏÖ½âñÏÂÃæÎÒÃÇÏÈÀ´Á˽âÒ»ÏÂÕâÖÖÉè¼ÆÄ£Ê½¡£
·¢²¼£¯¶©ÔÄģʽ£¨×îÓÅ·½°¸£©
·¢²¼£¯¶©ÔÄģʽÓÉÒ»¸ö·¢²¼Õß¡¢¶à¸ö¶©ÔÄÕßÒÔ¼°Ò»¸öµ÷¶ÈÖÐÐÄËù×é³É¡£¶©ÔÄÕßÃÇÏÈÔÚµ÷¶ÈÖÐÐĶ©ÔÄijһʼþ²¢×¢²áÏàÓ¦µÄ»Øµ÷º¯Êý£¬µ±Ä³Ò»Ê±¿Ì·¢²¼Õß·¢²¼ÁËÒ»¸öʼþ£¬µ÷¶ÈÖÐÐÄ»áÈ¡³ö¶©ÔÄÁ˸ÃʼþµÄ¶©ÔÄÕßÃÇËù×¢²áµÄ»Øµ÷º¯ÊýÀ´Ö´ÐС£

ÔÚ·¢²¼£¯¶©ÔÄģʽÖУ¬¶©ÔÄÕߺͷ¢²¼Õß²¢²»ÐèÒª¹ØÐĶԷ½µÄ״̬£¬¶©ÔÄÕßÖ»¹Ü¶©ÔÄʼþ²¢×¢²á»Øµ÷¡¢·¢²¼ÕßÖ»¹Ü·¢²¼Ê¼þ£¬ÆäÓàÒ»Çн»¸øµ÷¶ÈÖÐÐÄÀ´µ÷¶È£¬´Ó¶øÄÜʵÏÖ½âñî¡£
ÔÚ app ¿çÒ³ÃæÍ¨ÐÅÕâ¸öÎÊÌâÉÏ£¬iOS ¶ËµÄ Notification Center ¡¢°²×¿¶ËµÄ EventBus
£¬Ò²ÊÇͨ¹ýÕâÑùÒ»ÖÖÉè¼ÆÄ£Ê½È¥½â¾öµÄ£¬²»¹ý΢ÐÅС³ÌÐòÄÚ²¿²¢Ã»ÓÐÌṩÕâÖÖʼþ֪ͨ»úÖÆ£¬ËùÒÔÎÒÃÇÐèÒªÊÖ¶¯È¥ÊµÏÖÒ»¸ö¡£
ÎÒÃÇÊ×ÏÈҪʵÏÖÒ»¸ö Event À࣬ËüÓ¦¸Ãº¬ÓÐÒ»¸öÊÕ¼¯»Øµ÷º¯ÊýµÄ¶ÔÏ󣬺ÍÌṩÈý¸ö»ù´¡·½·¨£ºon£¨¶©ÔÄ£©¡¢
emit£¨·¢²¼£©¡¢ off£¨×¢Ïú£©¡£
// event.js
classEvent{
/**
* on ·½·¨°Ñ¶©ÔÄÕßËùÏëÒª¶©ÔĵÄʼþ¼°ÏàÓ¦µÄ»Øµ÷º¯Êý¼Ç¼ÔÚ Event ¶ÔÏóµÄ _cbs
ÊôÐÔÖÐ
*/
on (event, fn) {
if (typeof fn != "function") {
console.error('fn must be a function')
return
}
this._cbs = this._cbs || {}
;(this._cbs[event] = this._cbs[event] || []).push(fn)
}
/**
* emit ·½·¨½ÓÊÜÒ»¸öʼþÃû³Æ²ÎÊý£¬ÔÚ Event ¶ÔÏóµÄ _cbs ÊôÐÔÖÐÈ¡³ö¶ÔÓ¦µÄÊý×飬²¢Öð¸öÖ´ÐÐÀïÃæµÄ»Øµ÷º¯Êý
*/
emit (event) {
this._cbs = this._cbs || {}
var callbacks = this._cbs[event], args
if (callbacks) {
callbacks = callbacks.slice(0)
args = [].slice.call(arguments, 1)
for (var i = 0, len = callbacks.length; i <
len; i++) {
callbacks[i].apply(null, args)
}
}
}
/**
* off ·½·¨½ÓÊÜʼþÃû³ÆºÍµ±³õ×¢²áµÄ»Øµ÷º¯Êý×÷²ÎÊý£¬ÔÚ Event ¶ÔÏóµÄ _cbs ÊôÐÔÖÐɾ³ý¶ÔÓ¦µÄ»Øµ÷º¯Êý¡£
*/
off (event, fn) {
this._cbs = this._cbs || {}
// all
if (!arguments.length) {
this._cbs = {}
return
}
var callbacks = this._cbs[event]
if (!callbacks) return
// remove all handlers
if (arguments.length === 1) {
delete this._cbs[event]
return
}
// remove specific handler
var cb
for (var i = 0, len = callbacks.length; i <
len; i++) {
cb = callbacks[i]
if (cb === fn || cb.fn === fn) {
callbacks.splice(i, 1)
break
}
}
return
}
} |
¾ßÌåµ÷Ó÷½·¨
App ÊÇС³ÌÐòµÄʵÀý£¬ÔÚÿ¸ö Page Àï¶¼ÄÜͨ¹ýÖ´ÐÐ getApp º¯Êý»ñÈ¡µ½Ëü¡£ÎÒÃÇ¿ÉÒÔ°Ñ Event
ÀàµÄʵÀý¹ÒÔØÔÚ App ÖУ¬·½±ãÿ¸ö Page È¥µ÷Óá£
// app.js
const Event = require('./libs/event')
App({
event: new Event(),
...
}) |
¶©µ¥ÁбíÒ³ÔÚ onLoad ÉúÃüÖÜÆÚÖж©ÔÄ ¡°afterPaySuccess¡± ʼþ¡£
//order_list.js
var app = getApp()
Page({
onLoad: function(){
app.event.on('afterPaySuccess',this.afterPaySuccess.bind(this))
},
afterPaySuccess: function(orderId){
...
},
...
})
ÔÚ¶©µ¥ÏêÇéÒ³Ö§¸¶³É¹¦µÄ»Øµ÷ÖУ¬·¢²¼ ¡°afterPaySuccess¡± ʼþ,ͬʱ´øÉ϶©µ¥
id ²ÎÊý¡£
//order_detail.js
var app = getApp()
Page({
raisePayment: function(){
...
app.event.emit('afterPaySuccess', orderId)
},
...
}) |
ËùÓÐ Page µÄ onUnload ÉúÃüÖÜÆÚ£¬±ØÐë×¢Ïúµô֮ǰ¶©ÔĵÄʼþ¡£×¢Ïú·½·¨ off µÄµ÷ÓÃ×ËÊÆÓÐÈýÖÖ£¬²»¹ý»¹Êǽ¨Òé×¢Ïúµ±Ç°
Page Ëù¶©ÔĵÄʼþ£¬¶ø²»ÊÇ×¢ÏúËùÓеġ£
var app = getApp()
Page({
onUnload: function(){
// remove all
app.event.off()
// remove all callbacks
app.event.off('afterPaySuccess')
// remove specific callbacks
app.event.off('afterPaySuccess', this.afterPaySuccess)
},
...
}) |
µ½´Ë¾Í½áÊøÁËÂ𣿻¹Ã»ÓУ¬°´ÕÕÎÒÃǵĶ©ÔÄ¡¢×¢Ïúд·¨£¬ÔÚ×¢ÏúÖ¸¶¨»Øµ÷º¯ÊýµÄʱºò£¬ÆäʵÊÇÓÀÔ¶×¢Ïú²»Á˵ġ£
ÍêÉÆoff·½·¨
ΪÁËÈÃÿ¸ö»Øµ÷º¯Êý±»µ÷ÓÃʱµÄ this ¶¼Ö¸Ïò¶ÔÓ¦µÄ Page ¶ÔÏ󣬱ØÐëÔÚ¶©ÔÄʱ¶Ô»Øµ÷º¯Êý°ó¶¨µ±Ç°µÄÉÏÏÂÎĶÔÏó¡£
app.event.on('afterPaySuccess',this.afterPaySuccess.bind(this)) |
Ï൱ÓÚ
app.event.on('afterPaySuccess', function(){ var args = Array.prototype.slice.call(arguments) // fn¡¢that·Ö±ðΪ±Õ°üÆðÀ´µÄ»Øµ÷º¯ÊýºÍpage¶ÔÏó return fn.apply(that, args) }) |
ÕýÒòΪ bind ·½·¨»á·µ»ØÕâÑùÒ»¸öÄäÃûº¯Êý£¬È»ºóÕâ¸öÄäÃûº¯Êý»á±»¼ÓÈëµ½»Øµ÷Êý×éÖС£Òò´ËÎÒÃÇ×¢ÏúÖ¸¶¨»Øµ÷º¯ÊýµÄʱºò£¬Ôڻص÷Êý×éÖÐÊÇÕÒ²»µ½ËüµÄ£¬Ò²¾ÍÓÀÔ¶ÎÞ·¨×¢Ïú¡£
ΪÁ˱£³ÖÎÒÃÇÔÀ´µÄ emit µ÷Ó÷½Ê½£¬ÎÒÏë¹ýÖ±½Ó°Ñ Function.prototype.bind
¸Äд£º
Function.prototype.bind = function(that){ var fn = this var cb = function(){ var args = Array.prototype.slice.call(arguments) return fn.apply(that, args) } cb.fn = this return cb } |
È»ºóÔÙÉÔ΢ÐÞ¸ÄһϠoff ·½·¨ÀïµÄÅжÏÌõ¼þ
// remove specific callbacks ... if (cb === fn || cb.fn === fn) { callbacks.splice(i, 1) break } ... |
ÔÚä¯ÀÀÆ÷»·¾³ÕâÖÖ×ö·¨ÊÇ¿ÉÐе쬵«ÊÇÔÚС³ÌÐò²àÔòÊÇʧ°ÜµÄ¡£ÒòΪÎÒÃǶ¨ÒåµÄÕâЩ function ÔÚС³ÌÐòÀï²¢²»ÊÇ
Function µÄʵÀý£¬ÄÇÎÞÂÛÎÒÔõÑùÐÞ¸Ä Function µÄ prototype ÊôÐÔ£¬function
²¢²»»á¼Ì³Ðµ½¡£ÔÒòÊÇС³ÌÐò°Ñ Function ¸ø¸ÄдÁË£º
//console
Function.toString()
// "function (){if(arguments.length>0&&"return
this"===arguments[arguments.length-1])return
function(){return e}}" |
ÓÅ»¯·½°¸
ÔÚС³ÌÐò»·¾³ÖÐÊDz»ÄÜ͵ÀÁÁË£¬ÐèÒª°Ñ֮ǰµÄ´úÂë¸ÄдһÏ¡£Òª°Ñ Page ¶ÔÏóÒ²´«¸øµ÷¶ÈÖÐÐı£´æÆðÀ´£¬×÷Ϊ»Øµ÷º¯Êýµ÷ÓÃʱµÄÉÏÏÂÎĶÔÏó¡£
´úÂ룺
//event.js
classEvent{
on (event, fn, ctx) {
if (typeof fn != "function") {
console.error('fn must be a function')
return
}
this._stores = this._stores || {}
;(this._stores[event] = this._stores[event] ||
[]).push({cb: fn, ctx: ctx})
}
emit (event) {
this._stores = this._stores || {}
var store = this._stores[event], args
if (store) {
store = store.slice(0)
args = [].slice.call(arguments, 1)
for (var i = 0, len = store.length; i < len;
i++) {
store[i].cb.apply(store[i].ctx, args)
}
}
}
off (event, fn) {
this._stores = this._stores || {}
// all
if (!arguments.length) {
this._stores = {}
return
}
// specific event
var store = this._stores[event]
if (!store) return
// remove all handlers
if (arguments.length === 1) {
delete this._stores[event]
return
}
// remove specific handler
var cb
for (var i = 0, len = store.length; i < len;
i++) {
cb = store[i].cb
if (cb === fn) {
store.splice(i, 1)
break
}
}
return
}
} |
µ÷Ó÷½·¨Ò²ÐèÒª¸Äһϣ¬²»ÐèҪʹÓà bind ·½·¨ÁË£¬Ö»Ðè´«Èë Page ¶ÔÏó£º
app.event.on('afterPaySuccess', this.afterPaySuccess, this) |
дÔÚ×îºó
Ò»¸ö¼òµ¥µÄʼþÀ༸ʮÐдúÂë¾ÍÄÜдÍêÁË£¬µ«×÷Ϊһ¸ö»ù´¡Ä£¿é»¹ÊÇÓкܶà¿ÉÒÔÓÅ»¯ºÍÍØÕ¹µÄµØ·½¡£ÓÐÐËȤµÄͬѧ¿ÉÒÔÑжÁÒ»ÏÂ
nodejs µÄ event Ä£¿é: https://github.com/nodejs/node/blob/master/lib/events.js
£¬ÀïÃæ¶ÔʼþÀà×öÁ˺ܶàÓÅ»¯ºÍ¹¦ÄÜÍØÕ¹¡£
ÁíÍ⣬С³ÌÐòµÄ¿ª·¢¹ý³ÌÖÐÏàÐÅ´ó¼Ò¶¼Óöµ½Á˲»ÉٵĿӺͷ¢ÏÖÁËһЩ¿ÉÒÔÓÅ»¯µÄµã¡£ÎªÁËÊÕ¼¯ÕâЩ¿ª·¢ÖеÄÍ´µã£¬ÎÒÃÇ¿ªÁËÒ»¸ö
issue-list£º https://github.com/o2team/wxapp-issue-list/blob/master/issue-list.md
£¬»¶Ó´ó¼ÒǰÀ´Ìá issue~ |