简体   繁体   English

AngularJS将对象缓存设置为脏

[英]AngularJS setting an object cache as dirty

I'm using AngularJS 1.2.x for my frontend with xeditable to edit items inplace and update the database. 我将AngularJS 1.2.x用于xeditable来编辑项目并更新数据库。 My backend is Rails 3.2. 我的后端是Rails 3.2。

I have a level object which has many versions, each version has a from_date & cost_cents field in which I have setup xeditable to do inline editing. 我有一个具有多个版本的关卡对象,每个版本都有一个from_date和cost_cents字段,在其中我可以设置xeditable来进行内联编辑。 The problem is after I edit these, if I refresh the screen the old values show up. 问题是编辑这些内容后,如果刷新屏幕,则会显示旧值。 If I disable caching in the browser however I get the correct values. 但是,如果禁用浏览器中的缓存,则会得到正确的值。 So I need a way to flag the parent object's cache as dirty. 因此,我需要一种将父对象的缓存标记为脏的方法。

Here is an example of using xeditable on a date field for a level's version. 这是在级别字段的日期字段上使用xeditable的示例。

        <td editable-text="version.from_date"
            onbeforesave="update_version(level.id, version.id, $data, version.cost_cents)"
            class='date'>{{version.from_date}}</td>

Is it possible to get AngularJS or angular-xeditable to flag the parent object's cache as dirty so that it will reload the values from the database? 是否有可能使AngularJS或angular-xeditable将父对象的缓存标记为脏,以便从数据库中重新加载值?

Update: Using a form instead 更新:改用表格

I replaced xeditable with a form and still got the same problem with caching. 我用表单替换了xeditable,但仍然遇到了与缓存相同的问题。 This doesn't happen on my development server only on staging & production servers. 仅在登台和生产服务器上,这不会在我的开发服务器上发生。

    <form ng-submit="update_version(level.id, version.id, version.from_date, version.cost_cents)">
        Date: <input type='text' id="from_date" name="version.from_date" ng-model="version.from_date" />
        Cost: <input type='text' id="cost_cents" name="version.cost_cents"
                     ng-model="version.cost_cents"
                     form-cents-to-dollars />

        <button id="update_btn" type="submit" class="confirm_btn">
            <i class="fa fa-pencil"></i> Update Version
        </button>
        <td><a id="edit" href="/api#/levels/{{level.id}}/edit_version/{{version.id}}">Edit</a></td>
        <td><a id="delete" href="" ng-confirm-click="delete_version(version.id)">
            <i class="fa fa-trash-o fa-lg" /></a></td>
    </form>

Update: Deleting the cookie 更新:删除cookie

If I load the staging server in firefox and do the same test, then go and delete the cookie for the current session and refresh my versions listing it now has the correct data from the database displayed. 如果我将登台服务器加载到firefox中并进行相同的测试,则删除当前会话的cookie,并刷新我的版本,列出它现在具有从数据库显示的正确数据。

Update: Responds 304 Not modified 更新:回复304未修改

The response I get is http 304: Not Modified, when infact I did modify the data, but as it's a child object AngularJS hasn't picked it up. 我得到的响应是http 304:未修改,实际上我确实修改了数据,但是由于它是子对象,AngularJS尚未选择它。

Update: thoughts 更新:想法

I'm starting to think it might be Rails which is doing the caching? 我开始认为可能是Rails正在执行缓存?

By switching this flag to true in development I can now replicate the same problem in development mode. 通过在开发中将此标志切换为true,我现在可以在开发模式下复制相同的问题。 Which saves a lot of time. 这样可以节省很多时间。

  config.action_controller.perform_caching = true

Update: Hacky workaround 更新:Hacky解决方法

If I tag onto the end of my AngularJS http call a random number then it doesn't call cache and it works correctly everytime. 如果我在AngularJS的末尾标记http,则调用一个随机数,则它不会调用缓存,并且每次都能正常工作。 It's a bit hacky though and I would like a better way of doing this. 不过,这有点hacky,我希望有一种更好的方法。

$http({method: 'GET', url: "/levels/#{$stateParams['id']}?rnd="+new Date().getTime(), cache: false}).
  success( (data, status, headers, config) ->
    $scope.level = data
  )

将此行放在我不想缓存的控制器中;

headers['Last-Modified'] = Time.now.httpdate

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

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