简体   繁体   English

在骨干网和Rails之间共享过滤器,排序和验证逻辑

[英]Share filter, sort, and validation logic between backbone and rails

I find myself regularly having to repeat sorting, filtering, and validation logic across my client side and server side. 我发现自己经常不得不在客户端和服务器端重复排序,过滤和验证逻辑。 I want to have all this logic on my rails server side be replicated on backbone so I can do a get request or a client side filter and get the same stuff. 我希望将Rails服务器端的所有这些逻辑都复制到主干上,以便我可以执行get请求或客户端过滤器并获得相同的东西。 Same goes for sorting and validating. 排序和验证也是如此。

This may be a big question so if anyone has an idea of how to do any of these, I would really appreciate it. 这可能是一个很大的问题,因此,如果有人对如何做这些事情有任何想法,我将不胜感激。

Here's an example of what I mean. 这是我的意思的例子。 This is the backbone filter 这是骨干过滤器

bySearchTerm: (term) ->
  return @ if term == ''

  filtered = @filter((item) ->
  item.get("name").toLowerCase().indexOf(term.toLowerCase()) > -1 or item.get("number").toString().toLowerCase().indexOf(term.toLowerCase()) > -1
)
  new Evue.Collections.Customers(filtered)

But then I needed to replicate it on the server side so someone could do a get request and get the same filtered results. 但是随后我需要在服务器端复制它,以便有人可以执行get请求并获得相同的过滤结果。

unless params[:search_term].blank?
  @customers = @customers.where("lower(name) LIKE ? or number LIKE ?", "%#{params[:search_term].downcase}%", "%#{params[:search_term].downcase}%")
end

This gets out of hand when it comes to all the filters, sorting, and validations. 当涉及所有过滤器,排序和验证时,这将变得一发不可收拾。 Is switching over to node and using backbone for server and client side logic my answer? 切换到节点并使用骨干服务器和客户端逻辑是我的答案吗? Not sure, open to whatever. 不确定,可以接受任何内容。

I can suggest you to look into backbone.paginator - plugin for Backbone.js, which defines a few new types of collections with enhanced filtering, paging, sorting. 我建议您研究一下ribs.paginator -Backbone.js的插件,它定义了一些具有增强的过滤,分页和排序功能的新集合类型。

I like the approach with so called {mode: "client"} (this can help to avoid replication). 我喜欢使用所谓的{mode: "client"} (这有助于避免复制)。

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

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