什麼將實現提升一個簡單的CRUD接口,並使其電梯 - CRUD與設計師友好的模板和Ajax
- 設計師友好
- 阿賈克斯
一個乾淨的方式讓我們假設我們有一個觀點
<table data-lift="CrudList">
<tr>
<td role="data">Item goes here</td>
<td><button role="remove" type="button">remove</button></td>
</tr>
<tr class="clearable">
<td>Item two here</td>
<td><button type="button">remove</button></td>
</tr>
<tr class="clearable">
<td>Item three!</td>
<td><button type="button">remove</button></td>
</tr>
</table>
<form data-lift="form.ajax">
<div data-lift="CrudList.create">
<input type="text" name="text"></input>
<button type="submit"></button>
</div>
</form>
而且片斷
object CrudList {
def render = {
def remove(item: String) =() => {
ListDAO.remove(item)
JE.JsRaw("""Some JavaScript to remove <tr> from the UI""")
}
ClearClearable &
"tr *" #> ListDAO.all.map(item => {
"role=data" #> item &
"role=remove" #> ajaxInvoke(remove(item))
})
}
def create = {
var text = ""
def process(): JsCmd = {
val item = ListDAO.create(text)
JsCmds.Noop // TODO: replace this with some JsCmd
// that will create and populate new table row in the UI
// without polluting the snippet with markup
}
"@text" #> SHtml.text(text, s => text = s) &
"button *+" #> SHtml.hidden(process)
}
}
示例可能有錯誤,純粹是爲了演示。
render
片段非常簡單 - 我們修改現有標記並將列表呈現爲表格行。
我有點猶豫,要完成create
的片段。保留列表項的代碼很簡單,但我不知道如何使用新的<tr>
來更新<table>
。我想避免使用標記留下空間讓設計人員在桌子上做他們想要的事情。你會如何完成這個片段?