我的backbone.js中有以下模型-
var Book = Backbone.Model.extend({
defaults: {
id: 0,
title: 'Book',
price: 100,
quantity: 0
}
});
与集合为,
var BookList = Backbone.Collection.extend({
model: Book,
url: function () {
return 'Handler.ashx'
}
});
通过在这个主干中使用 collection.fetch() 方法,我可以使用以下 handler.ashx 从数据库中获取书籍列表
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports System.Web.Script.Serialization
Imports System.Data.OleDb
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim detail As List(Of MyBookList) = GetBookDetail()
If detail.Count > 0 Then
context.Response.ContentType = "application/json"
Dim json As String = New JavaScriptSerializer().Serialize(detail)
context.Response.Write(json)
Else
context.Response.StatusCode = 404
End If
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Protected Function GetBookDetail() As List(Of MyBookList)
Dim ConnectionString As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim sqlConn As New MySqlConnection(ConnectionString)
Dim sqlCommand As New MySqlCommand
sqlCommand.Connection = sqlConn
Dim dReader As MySqlDataReader
Dim dtExcelRecords As New DataTable()
Dim ds As New DataSet()
sqlConn.Open()
sqlCommand.CommandText = "SELECT * FROM booklist "
sqlCommand.CommandType = CommandType.Text
dReader = sqlCommand.ExecuteReader()
Dim book As New List(Of MyBookList)
If dReader.HasRows Then
While dReader.Read()
Dim bookObject As New MyBookList
bookObject.title = dReader(0)
bookObject.price = dReader(1)
bookObject.id = dReader(2)
bookObject.quantity = dReader(3)
book.Add(bookObject)
End While
End If
Return book
End Function
End Class
我想知道应该将哪些代码添加到backbone.js 和处理程序以更新书单集合中的特定模型。单击保存按钮时,我触发了以下保存方法..
updateBookTitle: function () {
var a = $('.title').val();
this.model.set({ title: a });
this.model.save();
},
保存时应如何修改处理程序 url 以发布模型 ID?