简体   繁体   English

Backbone.js与MVC 3和ViewModels

[英]Backbone.js with MVC 3 and ViewModels

So right now I'm trying to hook up Backbone.js into my MVC3 app, but I'm a little confused on how exactly to set it up. 所以现在我正在尝试将Backbone.js连接到我的MVC3应用程序中,但我对如何设置它有点困惑。

So, without using Backbone, I have Views, ViewModels, and Controllers. 因此,在不使用Backbone的情况下,我有Views,ViewModels和Controllers。 One ViewModel, AddBookViewModel, contains all the properties needed for a new book, including an IEnumerable for a select list to select a Publisher from the Publishers table. 一个ViewModel,AddBookViewModel,包含新书所需的所有属性,包括用于从Publishers表中选择Publisher的选择列表的IEnumerable。 When I save a new book, it contains a foreign key to the publisher. 当我保存新书时,它包含发布者的外键。

How exactly is something like this done using Backbone? 使用Backbone完成这样的事情到底是怎么回事? I have created a form just fine to test things out, where the Publisher is added on my Controller just before saving(its obviously always the same). 我已经创建了一个表单,可以很好地测试一下,在保存之前将Publisher添加到我的Controller上(它显然总是一样)。 Now I want to go ahead and add a Publishers field to my form. 现在我想继续向我的表单添加一个Publishers字段。

In my js file, am I supposed to make a new model and view for the Publishers drop down box, and bind the data to that one element? 在我的js文件中,我是否应该为Publishers下拉框创建一个新模型和视图,并将数据绑定到该一个元素? Does that mean that I make a ViewModel JUST for a drop down box? 这是否意味着我为下拉框创建了一个ViewModel? As well as an ActionResult on my Controller that returns only a list of Publishers for binding? 我的Controller上的ActionResult以及仅返回绑定的Publishers列表?

So based on my comment above, here is more information 所以基于我上面的评论,这里有更多信息

Backbone is a Backache IMO. Backbone是一个Backache IMO。 Pluralsight, the leading online video training authority has videos on Knockout.js and CoffeeScript , not on the backbone.js or javascriptmvc Pluralsight,领先的在线视频培训机构,有关于Knockout.js和CoffeeScript的视频,而不是关于backbone.js或javascriptmvc的视频

http://www.pluralsight-training.net/microsoft/Courses http://www.pluralsight-training.net/microsoft/Courses

Here is a link of someone comparing 3 to 4 of them (knockout, javascriptmvc, backbone and jmvc) 这是一个比较3到4个人的链接(淘汰赛,javascriptmvc,骨干和jmvc)

http://backbonefu.com/2011/09/knockout-vs-javascriptmvc-vs-backbone/ http://backbonefu.com/2011/09/knockout-vs-javascriptmvc-vs-backbone/

As a C# developer, this is a good read to think about writing javscript differently than how you write C# as it is easy to not have proper structure http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-3/ 作为一名C#开发人员,考虑编写javscript与编写C#的方式不同,因为很容易找不到合适的结构,这是一个很好的阅读http://enterprisejquery.com/2010/10/how-good-c-habits-可以-鼓励,坏JavaScript的习惯,部分-3 /

I just answered a similar question to this earlier. 我刚刚回答了类似的问题。 All your server should do is return json from your controllers. 您的所有服务器应该从您的控制器返回json。 Depending on the complexity of the application been built, I usually just have one view, which is my SPA (Single Page App/View). 根据构建的应用程序的复杂性,我通常只有一个视图,这是我的SPA(单页面应用程序/视图)。 Your application should be rendered using the json data returned from your controllers. 应使用从控制器返回的json数据呈现应用程序。 None of my controllers actually return views, except for the home controller. 除了家庭控制器之外,我的控制器都没有实际返回视图。 The rest of my application is built from the data that is being returned from my server. 我的应用程序的其余部分是根据从我的服务器返回的数据构建的。 Let the client do 85-90% of your work and the server the remaining 15-10% (the majority, if not all, returning the data required for your app). 让客户做85-90%的工作,服务器做剩余的15-10%(大多数,如果不是全部,则返回应用程序所需的数据)。

There have been cases, where my Backbone application actually sits on a different domain than my MVC application. 有些情况下,我的Backbone应用程序实际上位于与我的MVC应用程序不同的域上。 My Backbone application talks to my server application using the REST API. 我的Backbone应用程序使用REST API与我的服务器应用程序进行通信。

Hope this helps! 希望这可以帮助!

This might help.... I've provided this answer to another Backbone.js and ASP.net MVC question. 这可能会有所帮助....我已经为另一个Backbone.js和ASP.net MVC问题提供了这个答案。

I've compiled a couple of Backbone examples together into a working ASP.net MVC 3 application that is using REST interface. 我已经将一些Backbone示例编译成一个正在使用REST接口的ASP.net MVC 3应用程序。 Here is the link to my tumblr Blog where I have provided information to the source code and the websites I used as resources. 这是我的tumblr博客的链接,我在其中提供了源代码和我用作资源的网站的信息。 Backbone.js works really well with MVC 3 and I am always looking for new ways to push this example. Backbone.js与MVC 3的效果非常好,我一直在寻找推动这个例子的新方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM