在这种情况下使用的最佳软件设计是什么
我需要使用 jQuery 生成 HTML 片段。这些片段的创建取决于一些数据。数据存储在服务器端的会话中(使用 PHP)。
此刻我实现了这一目标
- 通过 AJAX 以 JSON 形式从服务器检索数据
- 并通过读取这些数据的特定 javascript 函数构建片段
问题是数据的复杂性越来越大,因此序列化为 JSON 变得更加困难,因为我无法自动完成。 我无法自动执行此操作,因为某些信息是合理的,因此我生成一个“剥离”版本以发送给客户端。
我知道如果没有任何代码可读,很难理解,但我希望这是一个常见的场景,并且很高兴您能给我任何提示、建议甚至设计模式。
我是否应该在服务器上存储完整数据和剥离数据,然后使用某个库从剥离数据自动生成 JSON?但这也意味着我必须同步这两个数据。
或者也许我可以将逻辑移动到服务器端,这样可以避免发送数据。但这意味着发送 javascript 代码(因为我依赖 jQuery)。也许不是一个好主意。
如果不清楚,请随时询问我更多详细信息。
感谢您的帮助
I need to generate HTML snippets using jQuery. The creation of those snippets depends on some data. The data is stored server-side, in session (where PHP is used).
At the moment I achieved this
- retrieving the data from the server via AJAX in form of JSON
- and building the snippets via specific javascript functions that read those data
The problem is that the complexity of the data is getting bigger and hence the serialization into JSON is getting even more difficult since I can't do it automatically.
I can't do it automatically because some information are sensible so I generate a "stripped" version to send to the client.
I know it is difficult to understand without any code to read, but I am hoping this is a common scenario and would be glad for any tip, suggestion or even design-pattern you can give me.
Should I store both a complete and a stripped data on the server and then use some library to automatically generate the JSON from the stripped data? But this also means I have to get the two data synchronized.
Or maybe I could move the logic server-side, this way avoiding sending the data. But this means sending javascript code (since I rely on jQuery). Maybe not a good idea.
Feel free to ask me more details if this is not clear.
Thank you for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有多种可用的 Javascript/jQuery 模板解决方案。 John Resig 正在开发一个即使不是核心发行版的一部分也可能成为流行的 jQuery 插件的产品。凯尔·辛普森(Kyle Simpson)也在做这样的事情。
我在谷歌上搜索了它的参考资料,但实际上我建议您自己搜索,因为那里有很多好的信息。
编辑这里有一个非常好的链接:http:// /www.west-wind.com/Weblog/posts/509108.aspx
There are several Javascript/jQuery templating solutions available. John Resig is working on one that's likely to become a popular jQuery add-on, if not part of the core distribution. Kyle Simpson is also doing one.
I googled for a reference to it, but really I'd suggest doing your own searching because there's lots of good information out there.
edit well here's a pretty good link: http://www.west-wind.com/Weblog/posts/509108.aspx
您可以使用 PHP 的
json_encode
和json_decode
方法将本机 PHP 对象转换为 JSON 数据表示形式。You can use PHP's
json_encode
andjson_decode
methods to convert native PHP objects into JSON data representation.