我正在寻找一个非常简单的示例,该示例显示将Knockback代码连接到通过RESTful服务连接的骨干模型。我正在使用ServiceStack | c #backend。下面的所有链接都太复杂,并且使用localStore而不是通过url的RESTful服务。我也更喜欢在Javascript而不是CoffeeScript中看到示例。
我的示例网址类似于 localhost / entities ,其中点击此将导致RESTful网络服务返回所有实体。使用 localhost / entity / 1 命中它将返回ID为1的实体。
_http://kmalakoff.github.com/knockback/index.html
_HTTPS://github.com/kmalakoff/knockback-reference-app/
_HTTPS://github.com/addyosmani/todomvc
以下是第一个链接上的knockback教程的示例:
Models, Collection, ViewModel, and Bindings:
// Generated by CoffeeScript 1.3.3
var model, view_model;
model = new Backbone.Model({
first_name: "Planet",
last_name: "Earth"
});
view_model = kb.viewModel(model);
view_model.full_name = ko.computed((function() {
return "" + (this.first_name()) + " " + (this.last_name());
}), view_model);
ko.applyBindings(view_model, $('#kb_view_model_computed')[0]);
但是没有提及如何将骨干模型连接到RESTful Web服务。
有一些例子说明如何通过Backbone进行此操作,但我不确定使用Knockback时情况如何变化。
找到了以下链接,但没有帮助:
_http://stackoverflow.com/questions/7992431/using-knockoutjs-backbone-together
_http://stackoverflow.com/questions/9704220/is-knockback-js-production-ready
_http://stackoverflow.com/questions/10434203/defining-models-on-server-side-when-using-mvvm-with-knockout-js
提前感谢您提供的任何帮助。顺便说一句,你不希望你得到下划线的超链接......;)
答案 0 :(得分:7)
在伟大的Knockback项目的Kevin Malakoff的大力帮助和支持下,我得到了一个小例子!我使用ServiceStack Backbone Todos项目作为起点。
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<Person>
{
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<Person> people = new List<Person> { 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<Person>("/persons")
.Add<Person>("/persons/{Id}");
}
}
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
}
}
html文件: default.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MyApp2</title>
<script>window.JSON || document.write('<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js">\x3C/script>')</script>
<script src="Scripts/jquery-1.8.0.js" type="text/javascript" ></script>
<script src="Scripts/knockback-full-stack-0.16.1.js" type="text/javascript" ></script>
<script src="myapp.js" type="text/javascript" ></script>
</head>
<body>
<div id="myapp">
<div class="title">
<h1>MyApp</h1>
</div>
<div class="content">
<div id='kb_observable'>
<p>First name: <input class='text' data-bind="value: firstName" /></p>
<p>Last name: <input class='input-small pull-right' data-bind="value: lastName" /></p>
<p>Hello, <span data-bind="text: fullName"></span>!</p>
</div>
<div id="kb_collection_observable">
<div data-bind="if: persons">
<span>Has Persons</span>
</div>
<div data-bind="foreach: persons">
<p>First name: <input class='text' data-bind="value: firstName" /></p>
<p>Last name: <input class='input-small pull-right' data-bind="value: lastName" /></p>
</div>
</div>
</div>
</div>
</body>
</html>
javascript文件: 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]); });
答案 1 :(得分:1)
我把一个非常简单的例子放在一起。它假设您已经知道如何使用主干和淘汰赛,所以这只是一个快速举例说明它们如何一起使用
http://coder2.blogspot.com/2013/02/backbonejs-and-knockoutjs.html