有人可以帮我弄这个吗?

时间:2013-09-24 07:19:46

标签: backbone.js

我的骨干中有以下模型.js -

var Book = Backbone.Model.extend({

    defaults: {
        id: 0,
        title: 'Book',
        price: 100,
        quantity: 0
    }  

});

with collection as,

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和handler来更新booklist collection中的特定模型。当点击保存按钮时,我有以下保存方法三角形。

updateBookTitle: function () {
        var a = $('.title').val();
        this.model.set({ title: a });
        this.model.save();        
    },

如何修改处理程序网址以在保存时发布模型ID?

1 个答案:

答案 0 :(得分:0)

Backbone旨在使用RESTful服务,而WebForms则不然。使用.NET,您应该考虑使用WebAPI或MVC框架。如果您必须使用网络表单,则需要修改Backbone sync的工作方式,或者您必须根据请求的HTTP在.aspx中手动编写操作代码方法:GET =列表,POST =保存,PUT =更新,DELETE =删除。