简体   繁体   English

设置cookie后,Ember模板没有重新加载

[英]Ember template not reloading after i set cookie

I have a little problem. 我有一点问题。 I have a "use-button" which sets a cookie, telling that this deal is used. 我有一个“使用按钮”,它设置一个cookie,告诉我使用了这笔交易。 The button switch state to disabled. 按钮开关状态为禁用。

The problem is that, if you go back and forwards, the button is no longer disabled and you can still use the button. 问题是,如果你前后退,按钮不再被禁用,你仍然可以使用按钮。 If you refresh the page the button will be disabled. 如果刷新页面,该按钮将被禁用。

This is my code: 这是我的代码:

 export default Ember.Controller.extend(MobileHelper, { goodie: Ember.computed.alias('model'), tracker: Ember.inject.service(), used: undefined, fieldUsed: function(){ var pack_id = this.get('goodie.packContents.firstObject.id'); var cookies; if (this.cookie.getCookie('gp_pv') === undefined) { return false; } else { cookies = JSON.parse(this.cookie.getCookie('gp_pv')); } return cookies[String(pack_id)] === 1; }.property('goodie.packContents.firstObject.id'), profileComponent: function() { return `goodie-${this.get('goodie.type')}-profile`; }.property('goodie.type'), actions: { markSwiped: function() { var cookies; if (confirm("Er du sikker?")){ if (this.cookie.getCookie('gp_pv') === undefined){ cookies = {}; } else { cookies = JSON.parse(this.cookie.getCookie('gp_pv')); } var pack_id = this.get('goodie.packContents.firstObject.id'); if (cookies[String(pack_id)] !== 1){ cookies[String(pack_id)] = 1; } jQuery("#itemUsable").hide(); jQuery("#itemUsed").show(); this.get('tracker').trackGoodieExternalLinkClick(this.get('goodie')); this.cookie.setCookie('gp_pv', JSON.stringify(cookies), { expires: 50000, path: '/' }); this.container.lookup('view:toplevel').rerender(); route.transitionTo("index") } } } }); 
  {{#if fieldUsed}} <style> #itemUsable {display:none;} #itemUsed {display:block;} </style> {{else}} <style> #itemUsable {display:block;} #itemUsed {display:none;} </style> {{/if}} <div class="container" id="itemUsed"> <div class="goodie-page-swipe-action"> <div class="row"> <div class="col-xs-offset-3 col-xs-6"> <div class="btn gp-btn-primary btn-block btn-lg disabled">{{ t 'goodie.used'}}</div> </div> </div> </div> </div> <div class="container" id="itemUsable"> <div class="goodie-page-swipe-action"> <div class="row"> <div class="col-xs-offset-3 col-xs-6"> <div {{ action 'markSwiped' }} class="btn gp-btn-primary btn-block btn-lg">{{ t 'goodie.use'}}</div> </div> </div> </div> </div> 

Computed property values are normally cached and only updated when the values of the dependent keys change. 计算属性值通常被缓存,并且仅在从属键的值更改时才更新。 From the provided code it is not immediately obvious that, when markSwiped gets run, goodie.packContents.firstObject.id immediately changes. 从提供的代码中可以看出,当markSwiped运行时, goodie.packContents.firstObject.id立即发生变化。 I guess it doesn't, since fieldUsed and thus your templates do not get updated (which means the cookie value that you do update is never re-evaluated, until a full page refresh). 我想它没有,因为fieldUsed因此你的模板没有得到更新(这意味着你更新的cookie值永远不会被重新评估,直到整页刷新)。

Things that come to mind that you could try: 想到的事情你可以尝试:

  • You could try to make fieldUsed a volatile property which would hopefully cause it to be re-evaluated on every use (no longer cached) 你可以尝试使fieldUsed成为一个volatile属性 ,希望它能在每次使用时重新评估(不再缓存)

     fieldUsed: function() {...}.volatile() 
  • Make fieldUsed depend on some other value instead, or in addition. 使fieldUsed取决于其他值,或者另外。 For instance, as a crutch you could test some extraneous counter type variable that simply gets incremented somewhere in markSwiped 例如,作为一个拐杖,你可以测试一些无关的计数器类型变量,它只是在markSwiped某处markSwiped

     crutchCounter: 0, fieldUsed: function() {}.property('goodie.packContents.firstObject.id', 'crutchCounter'), markSwiped: function() {...; this.incrementProperty('crutchCounter'); ...} 

If either of these work, you can then also forego the jQuery DOM manipulation you have going on in markSwiped . 如果其中任何一个工作,那么您也可以放弃在markSwiped的jQuery DOM操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM