JSRender和AJAX和PHP [英] JSRender and AJAX and PHP

查看:91
本文介绍了JSRender和AJAX和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,在加载时会调用Document.ready jQuery脚本,该脚本使用今天的日期作为输入,将AJAX请求发送到PHP脚本,以查询数据库中该日期的任何条目".如果找到它们,则将它们添加到数组中,然后进行JSON编码并返回到jQuery脚本,然后在for循环中,使用JSRender模板在每个条目"中吐出<div>.这些<div>位于表单内.

I have a page, when loaded, calls a Document.ready jQuery script, which, using today's date as an input, sends an AJAX request to a PHP script to query the database for any "entries" with that date. If it finds some, they are added to a array, then JSON encoded and returned to the jQuery script, which then, in a for loop, spits out a <div> per "entry" using JSRender templates. These <div>s live inside a form.

有一个按钮可创建一个新的条目",按下该按钮将克隆最后一个条目",将其擦除,为输入数组(输入名称="TI [new1] []")提供一个新索引( ("new1"位)并将其附加在最后一个(在格式内)下方.提交表单后,会发出另一个AJAX请求以写入/更新数据库.

There is a button to create a new "entry" which when pressed just clones the last "entry", wipes it clean, gives the inputs arrays (input name="TI[new1][ ]") a new index (the 'new1' bit) and appends it under the last one (within the form). When form is submitted another AJAX request is made to write/update the database.

我无法弄清楚的是,在创建新条目后如何重新加载表单,以便它能够获取当前信息以及具有正确ID的新条目(如当前提交的表单),但当然不会刷新,因此这些条目仍然具有"newx" ID,而不是现在写入数据库时​​已分配的ID.

What I cannot quite figure out is how to reload the form when a new entry has been made, so that it gets current plus new entries with their correct ids, as at present the form submits, but of course does not refresh, so the entries still have the "newx" id rather than the one they have now been assigned when written to the database.

<form>
  <div id="empty-for-jsrender-output">
  </div>
  <script (template for jsrender)></script>

</form>

<script src="get-entries"></script>


// get-scripts
calls ajax with var for current date
returns json array
for each nested array, calls jsrender script, which spits out a div block per entry within #empty-for-jsrender-output

那么我该如何重新提交表单或#empty-for-jsrender-output,就像我在提交带有新条目的表单后第一次首次加载页面时所做的那样?

So how do I make the form or #empty-for-jsrender-output reload like it does when the page is first loaded for the first time after I submit the form with a new entry?

抱歉,编辑,忘了补充说,我已经在ajax.done部分的Submit-entry js脚本中尝试过此操作:

Sorry, edit, forgot to add that I have tried this in my submit-entry js script in the ajax.done section:

$.getScript("assets/js/getEntries.js");

尝试再次运行与实际重新加载页面时运行的脚本相同的脚本(有效).但是,所有这些操作在div中都是空的...不再填充它.

to try and run the same script again that runs when the page is actually reloaded (which works). But all this does is empty the div...it is not populating it again.

推荐答案

由于涉及特定的应用程序选择,因此我不会尝试提出有关此操作的详细信息.

I won't try to propose details of how to do that - since it involves specific application choices.

但是对于整体方法,我鼓励您使用JsViews,而不仅仅是JsRender,因此由于可观察到的数据更改和数据绑定,您可以获取UI的增量更新.

But for the overall approach, I would encourage you to use JsViews, not just JsRender, so you can get incremental updates of the UI thanks to observable data changes and data binding.

然后,对于您的AJAX场景,一种好方法是使用JsRender/JsViews中的新 compiled View Models 功能,并在获取更新的JSON时对数据调用merge()方法从第二个及后续AJAX请求返回的数据.

Then, for your AJAX scenario, a great approach is to use the new compiled View Models feature in JsRender/JsViews, and call the merge() method on your data when you get the updated JSON data returned from the second and subsequent AJAX requests.

以下是一些文档链接:

  • http://www.jsviews.com/#jsvmodel
  • http://www.jsviews.com/#viewmodelsapi@merge
  • http://www.jsviews.com/#jsvviewmodelsapi@mergesample

这篇关于JSRender和AJAX和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆