简体   繁体   English

ExtJS 4.1-Store.add()(之后是同步)与Model.save()

[英]ExtJS 4.1 - Store.add() (followed by sync) vs Model.save()

FIRST: I realize this question has been asked here: in ExtJS, is it better to call Model.save() or Store.Sync()? 首先:我意识到这里已经问过这个问题: 在ExtJS中,调用Model.save()或Store.Sync()更好吗? - however I wish to examine this further, specifically regarding minimizing XHR's and unnecessary overhead on both the client and server. -但是,我希望对此进行进一步的研究,特别是在最小化XHR和客户端和服务器上不必要的开销方面。 I do not feel either of these points were addressed in the linked question. 我认为链接的问题中没有提到这些观点。

I have a somewhat large application designed for enterprise resource management, consisting of many models, views and controllers. 我有一个为企业资源管理而设计的大型应用程序,其中包含许多模型,视图和控制器。 I handle all responses from my server by establishing a listener to Ext.Ajax requestComplete and requestException events. 我通过建立对Ext.Ajax requestCompleterequestException事件的侦听器来处理服务器的所有响应。 I took this approach rather than writing duplicate event handlers on every model's proxy afterRequest event. 我采用这种方法,而不是在每个模型的代理afterRequest事件上编写重复的事件处理程序。 This enables me to have all of my back-end (using the Zend Framework) controllers responding with three parameters: success , message and data . 这使我能够使所有后端控制器(使用Zend Framework)以三个参数响应: successmessagedata

After a successful request (ie, HTTP 200), the method run for requestComplete will inspect the JSON response for the aforementioned parameters. 成功请求后(即HTTP 200),为requestComplete运行的方法将检查JSON响应中的上述参数。 If success is false , it is expected that there will be an error message contained in message , which is then displayed to the user (eg 'There was a problem saving that product. Invalid product name'). 如果successfalse ,预计将有包含在错误信息message ,然后显示给用户(例如,“在保存该产品的一个问题。无效的产品名称”)。 If success is true, action is taken depending on the type of request, ie, Create, Read, Update or Destroy. 如果成功为真,则根据请求的类型采取操作,即创建,读取,更新或销毁。 After a successful create , the new record is added to the appropriate data store, after delete the record is destroyed, and so forth. create成功后,新记录将添加到适当的数据存储中,删除记录后将其删除,依此类推。

I chose to take this approach rather than adding records to a store and calling the store's sync method in order to minimize XHR's and otherwise round trips. 我选择采用这种方法,而不是将记录添加到商店并调用商店的sync方法,以最大程度地减少XHR和往返行程。 My current means of saving/updating data is to send the request to the backend and react to the result on the Ext front end. 我当前保存/更新数据的方法是将请求发送到后端,并对Ext前端的结果做出反应。 I do this by populating a model with data and calling model.save() for create/update requests, or model.destroy() to remove the data. 我通过用数据填充模型并调用model.save()来创建/更新请求,或调用model.destroy()来删除数据来做到这一点。

I found that when adding/updating/removing records from a store, then calling store.sync(), I would have to react to server's response in a way that felt awkward. 我发现,当从存储中添加/更新/删除记录,然后调用store.sync()时,我将不得不以一种尴尬的方式对服务器的响应做出反应。 Take for example, deleting a record: 例如,删除一条记录:

  1. First, remove the record from the store via store.remove() 首先,通过store.remove()从商店中删除记录
  2. Invoke store.sync() as I have store's autoSync set to false . 当我将store的autoSync设置为false调用store.sync()
  3. This fires the AJAX destroy request from the store's model proxy. 这会触发商店的模型代理发出的AJAX销毁请求。
  4. Here's where it gets weird.... if there is an error on the server while dropping the row from the database, the response will return success: false , however the record will have already been removed from the ExtJS Data Store. 这就是奇怪的地方。...如果从数据库中删除行时服务器上出现错误,则响应将返回success: false ,但是该记录已从ExtJS数据存储中删除。
  5. At this point, I can either call store.sync() , store.load() (both requiring a round trip) or get the record from the request and add it back to the store followed by a commitChanges() to avoid calling an additional sync/load and thus avoiding an unnecessary round trip. 在这一点上,我可以调用store.sync()store.load() (都需要往返)或从请求中获取记录并将其添加回存储区,然后再添加commitChanges()以避免调用额外的同步/加载,从而避免了不必要的往返。

The same goes for adding records, if the server fails somewhere while adding data to the database, the record is still in the ExtJS store and must be removed manually to avoid a round trip with store.sync() or store.load() . 添加记录也是如此,如果服务器在向数据库添加数据时某处发生故障,则该记录仍位于ExtJS存储中,必须手动删除该记录以避免与store.sync()store.load()往返。

In order to avoid this whole issue, as I previously explained, I instantiate one of my model objects (eg a Product model), populate it with data, and call myModel.save() . 为了避免出现整个问题,如前所述,我实例化了一个模型对象(例如Product模型),并用数据填充它,然后调用myModel.save() This, in turn, invokes the proxy's create or update depending on the ID of the model, and fires the appropriate AJAX request. 反过来,这取决于模型的ID调用代理的createupdate ,并触发适当的AJAX请求。 In the event that the back-end fails, the front-end store is still unchanged. 如果后端发生故障,则前端存储仍保持不变。 On successful requests (read: success: true , not HTTP 200), I manually add the record to the store and invoke store.commitChanges(true) , effectively syncing the store with the database without an additional round trip and avoiding unnecessary overhead. 对于成功的请求(读取: success: true ,不是HTTP 200),我手动将记录添加到存储中并调用store.commitChanges(true) ,从而有效地将存储与数据库同步,而无需进行额外的往返操作,并避免了不必要的开销。 For all requests, the server will respond with the new/modified data as well as a success parameter, and conditionally a message to display on the client. 对于所有请求,服务器将使用新的/修改的数据以及成功参数进行响应,并有条件地在客户端上显示一条消息。

Am I missing something here, or is this approach a good way to minimize XHR's and server/client overhead? 我是否在这里缺少任何东西,还是这种方法是最小化XHR和服务器/客户端开销的好方法? I am happy to provide example code should that be requested, however I feel that this is a rather general concept with fundamental code. 我很乐意提供应要求提供的示例代码,但是我认为这是基本代码的相当笼统的概念。

I think you have eloquently argued your position. 我认为你雄辩地主张了自己的立场。 I don't see anything wrong with the position you have taken. 我认为您担任的职位没有任何问题。 My only reproach is to point out that autoSync setting on a store that backs editable grid is a far less verbose way of accomplishing the task, albeit with less control. 我唯一的缺点是要指出,支持可编辑网格的商店上的autoSync设置是完成任务的冗长方式,尽管控制较少。

To add, the overhead which you point out is typically due to the unexpected or I would call edge cases that may need special handling or an extra refresh of data. 补充一点,您指出的开销通常是由于意外情况造成的,或者我称之为可能需要特殊处理或额外刷新数据的极端情况。 You could add listeners for those specific cases and leave the rest functioning with terse defaults. 您可以为这些特定情况添加侦听器,而使其余功能保持默认默认设置。

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

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