开发者

Rails best practices for writing .js.erb files

Sorry if this question has been asked before

I've started using a lot of js.erb files in my rails apps and I can't really find any information on the best practices for this sort of t开发者_开发百科hing. Right now I'm cobbling bits an pieces of other people's code

I know when and why to use js.erb, but I'm looking for a total dpsht's quide on how to write js.erb - syntax rules, do's and don't's etc


When I started I found I often used js.erb to transfer data from the server to the client, i.e:

var username = <%= user.name %>;

Using one interpreted language to dynamically write code in another interpreted language felt very wrong, so now I rarely use js.erb. Instead I set the server actions to output data in JSON format, and then use Javascript on the client to download the JSON and parse it into Javascript objects, so code and data stay separate.


you shouldn't use .js.erb because it will meet serious problem when user.name is changed. if you want to pass variable to js you can try gem client variable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜