The simplest example of Knockback js working with a RESTful webservice such as ServiceStack?

后端 未结 2 1721
醉梦人生
醉梦人生 2021-02-06 08:42

I am looking for a VERY simple example that shows wiring up Knockback code to a backbone model that connects via RESTful service. I am using ServiceStack|c# backend. All of th

2条回答
  •  感动是毒
    2021-02-06 09:28

    With much help and support from Kevin Malakoff from the great Knockback project I was able to get a small example working! I used the ServiceStack Backbone Todos project as a starting point.

    c# file: Global.asax.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;
    
    using ServiceStack.Redis;
    using ServiceStack.ServiceInterface;
    using ServiceStack.WebHost.Endpoints;
    
    namespace MyApp
    {
        public class Person
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    
        public class PersonService : RestServiceBase
        {
            public static Person kevin = new Person { Id = 1, FirstName = "Kevin", LastName = "Malakoff" };
            public static Person scott = new Person { Id = 2, FirstName = "Scott", LastName = "Idler" };
            public static List people = new List { kevin, scott };
    
            public override object OnGet(Person person)
            {
                if (person.Id != default(int))
                    return people[person.Id-1];
                return people;
            }
    
            public override object OnPost(Person person)
            {
                return base.OnPost(person);
            }
    
            public override object OnPut(Person person)
            {
                return OnPost(person);
            }
    
            public override object OnDelete(Person person)
            {
                return base.OnDelete(person);
            }
        }
    
        public class AppHost : AppHostBase
        {
            public AppHost() : base("MyApp", typeof(PersonService).Assembly) { }
    
            public override void Configure(Funq.Container container)
            {
                ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
                Routes
                  .Add("/persons")
                  .Add("/persons/{Id}");
            }
        }
    
        public class WebApiApplication : System.Web.HttpApplication
        {
            protected void Application_Start(object sender, EventArgs e)
            {
                new AppHost().Init();
            }
        }
    }
    

    html file: default.html

    
    
      
        MyApp2
        
        
        
        
      
    
        

    MyApp

    First name:

    Last name:

    Hello, !

    Has Persons

    First name:

    Last name:

    javascript file: myapp.js

    $(function() {

    //model
    var PersonModel = Backbone.Model.extend({ urlRoot: '/MyApp/persons' });
    var model = new PersonModel({ id: 1 });
    model.fetch();
    
    //viewmodel
    var PersonViewModel = function (person) {
    
        this.firstName = kb.observable(person, 'firstName');
        this.lastName = kb.observable(person, 'lastName');
        this.fullName = ko.computed((function () {
            return "" + (this.firstName()) + " " + (this.lastName());
        }), this);
    };
    var personViewModel = new PersonViewModel(model);
    
    //binding
    ko.applyBindings(personViewModel, $('#kb_observable')[0]);
    
    //model
    var PersonsModel = Backbone.Collection.extend({ model: PersonModel, url: '/MyApp/persons' });
    var personsModel = new PersonsModel();
    personsModel.fetch();
    
    //viewmodel
    var PersonsViewModel = function (persons) {
        this.persons = kb.collectionObservable(persons)
    };
    var personsViewModel = new PersonsViewModel(personsModel);
    
    //binding
    ko.applyBindings(personsViewModel, $('#kb_collection_observable')[0]); });
    

提交回复
热议问题