需要Knock JS和JQuery Tutorial帮助

时间:2011-08-13 23:11:46

标签: javascript jquery knockout.js

http://net.tutsplus.com/tutorials/javascript-ajax/into-the-ring-with-knockout-js/

我已经致力于第2轮 - 创建视图,使用网站上发布的代码,我通过FF FB 1.7.3返回错误

  括号中的

错误:缺失)   源文件:http://testing:8888/knockout/js/behavior.js   行:7,列:3   源代码:     }; ko.applyBindings(视图模型);

行为

(function ($) { var model = [{  name: "John",  address: "1, a road, a town, a county, a postcode",  tel: "1234567890",  site: "www.aurl.com", pic: "/i/john.jpg",  deleteMe: function () { viewModel.people.remove(this); }
}, {  name: "Jane",  address: "2, a street, a city, a county, a postcode",  tel: "1234567890",  site: "www.aurl.com",  pic: "/i/jane.jpg",  deleteMe: function () { viewModel.people.remove(this); }
}, {  name: "Fred",  address: "3, an avenue, a village, a county, a postcode",  tel: "1234567890",  site: "www.aurl.com",  pic: "/i/fred.jpg",  deleteMe: function () { viewModel.people.remove(this); }
}, {  name: "Freda",  address: "4, a street, a suburb, a county, a postcode",  tel: "1234567890",  site: "www.aurl.com",  pic: "/i/jane.jpg",  deleteMe: function () { viewModel.people.remove(this); }
}], viewModel = { people: ko.observableArray(model),
} }; ko.applyBindings(viewModel); })(jQuery);

1 个答案:

答案 0 :(得分:1)

教程中的代码存在缺陷。 behavior.js代码在viewModel和ko.applyBindings之间有一个额外的括号。它看起来像这样:

  

viewModel = {people:ko.observableArray(model),
  } }; ko.applyBindings ...

它需要看起来像这样:

  

viewModel = {people:ko.observableArray(model),
  }; ko.applyBindings ...

我不喜欢这个样本的几件事。这是我的版本,略有修改:

(function ($) { 
    var model, viewModel;
    model = [
        {
           name:"John",
           address:"1, a road, a town, a county, a postcode",
           tel:"1234567890",
           site:"www.aurl.com",
           pic:"/img/john.jpg",
           deleteMe:function ()   {
              viewModel.people.remove(this);
           }
        },
        {
           name:"Jane",
           address:"2, a street, a city, a county, a postcode",
           tel:"1234567890",
           site:"www.aurl.com",
           pic:"/img/jane.jpg",
           deleteMe:function ()   {
              viewModel.people.remove(this);
           }
        },
        {
           name:"Fred",
           address:"3, an avenue, a village, a county, a postcode",
           tel:"1234567890",
           site:"www.aurl.com",
           pic:"/img/fred.jpg",
           deleteMe:function ()   {
              viewModel.people.remove(this);
           }
        },
        {
           name:"Freda",
           address:"4, a street, a suburb, a county, a postcode",
           tel:"1234567890",
           site:"www.aurl.com",
           pic:"/img/jane.jpg",
           deleteMe:function ()   {
              viewModel.people.remove(this);
           }
        }
    ];
    viewModel = {
        people: ko.observableArray(model)  
    } ; 
    ko.applyBindings(viewModel);  
})(jQuery);