开发者

logic in rails controller vs in javascript (.js.erb)

I have a large "data" active record object whose info I filter based on what the user clicks on a page, then I display that filtered data to the user. I was wondering where the best place to put the data-filtering logic is. Right now I do the filtering in the rails controller action, so that in the view I only have to say something like

<script>
var filtered_data = <%= raw @filtered_data %>
alert('first piece of data is: '+filtered_data[0])
var filtered_names = <%= raw @filtered_names %>
</script>

and other such vars.

or, I was considering just passing the whole @data like:

<script>开发者_如何学JAVA;
var data = <%= data.to_json.html_safe %>
var filtered_data = **some js logic to filter data var**
var filtered_names = **more js logic to filter for names, maybe using the jquery .map function, etc **
</script>

which way is better for performance? I would think client-side JS is better, but it's probably not client side anymore since it's a .js.erb? Or would having more logic in the js.erb still help a bit?

Thanks for any insight!


you should put as few logic as possible into your view... if you filter the data via js, that data is beeing sent to the client (producing traffic).. so it is probably better to do the filtering on the server side.

always try just to display things in your view - this makes your code much more maintainable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜