2017-02-13 73 views
0

我的Riot.js observable的工作方式如下。無法獲得Riot.js可觀察到的通過回調傳遞參數

在我的「全球頭」標籤riot.store防暴全球範圍內觀察到的被更新(用戶輸入的輸入領域的一個新的高度),然後觀察的觸發器「update_dims」:

save_height() { 
    riot.store.cardHeight = this.refs.input_card_height.value 
    riot.store.trigger('update_dims') 
} 

在我的'卡片'標籤裏面,riot.store監聽'update_dims'併成功更新界面中的{myCardHeight}。

// function updates the card height 
update_cardHeight() { 
    this.myCardHeight = riot.store.cardHeight 
    this.update() 
} 
// observable runs when triggered and calls above function 'update_cardHeight' 
riot.store.on('update_dims',this.update_cardDimensions) 

但是,如果我嘗試直接從riot.store.trigger傳遞參數:

save_height() { 
    riot.store.cardHeight = this.refs.input_card_height.value 
    riot.store.trigger('update_dims','450') 
} 

下觀察到的不更新,即使me.myCardHeight變量已經用新更新的界面高度參數:

me = this 
riot.store.on('update_dims', function (height) { 
    me.myCardHeight = height 
    console.log(me.myCardHeight) 
    me.update() 
}) 

什麼是做到這一點的正確方法?

回答

0

只要你的店是可觀的暴亂這應該工作:

riot.store = riot.observable(); 

var me = this 
riot.store.on('update_dims', function (height) { 
    me.myCardHeight = height; 
    console.log(me.myCardHeight); 
    me.update(); 
}); 

riot.store.trigger('update_dims', 450); 

如果你在將其存儲在一個對象很感興趣,然後我寧願做一個共享存儲與性能的它的擁有:

var me = this 
me.myCardHeight = opts.store.height; 

me.opts.store.on('update_dims', function (height) { 
    me.opts.store.height = height; 
    me.myCardHeight 
    console.log(me.myCardHeight); 
    me.update(); 
}); 

var store = { 
    observable: riot.observable(), 
    height: 25 // default 
} 

riot.mount('myCard', {store: store}) 
標籤 myCard

然後此外

this.opts.trigger('update_dims', 450); 

,檢查出RiotControl可與這種類型的結構:現在,當你調用這個你會傳遞一個新的共享的高度。

希望這會有所幫助!