Backbone.js 与 ASP.NET MVC

发布于 2024-12-01 00:30:47 字数 415 浏览 0 评论 0原文

在过去的几天里,我一直在阅读有关 Backbone.js 的内容以及它如何简化 JS 代码与视图元素的交互(基本上是在 HTML 中)。我还了解了 KnockoutJS 和 Backbone.js 之间的差异。现在我在想,使用其中之一是否不可避免地会导致我们在视图中复制 MVC 应用程序中已有的代码(主要是 ViewModel 和 global.asax 中的路由)。本质上要求我们在 Backbone 或 Knockout 中编写另一组模型。据我了解,对于 KnockoutJS,这种情况更加普遍,这就是为什么我认为我会选择 Backbone,但现在我认为它并没有那么不同 - 在几个例子之后,我看到相同的重复变得越来越明显。

另外,如果我们已经有一堆 MVC 部分视图,现在我们应该使用一些模板引擎(如 JQuery 模板)在 Backbone 中重新创建它们,那么我们如何维护这样的应用程序?

In the last few days I've been reading about Backbone.js and how it simplifies JS code interaction with View elements, basically within HTML. I've also read about the differences between KnockoutJS and Backbone.js. Now I'm thinking whether using one or the other inevitably leads to duplicating the code that we already have in our MVC app (mostly ViewModels and routes in global.asax) inside our Views. Essentially requiring us to code another set of Models in Backbone or Knockout. As I understand, with KnockoutJS this is even more prevalent, that's why I thought I will choose Backbone but now I think it is not that different - after a few examples I saw that same duplication is becoming evident.

Also how do we maintain such an application if for instance we already have a bunch of MVC Partial Views and now we are supposed to recreate them in Backbone using some templating engine like JQuery templates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

聊慰 2024-12-08 00:30:47

我自己最近一直在阅读有关backbone和knockoutjs的内容,并且也在思考如何通过asp.net mvc来利用该框架。
避免模​​型重复的一种方法是序列化 (json) 服务器端视图模型并将其用作骨干或淘汰模型。积极的副作用是,您的客户端模型已经包含页面加载时的数据,并且无需在页面首次加载时通过 ajax 请求获取数据。
我知道仅序列化服务器端视图模型对于主干/淘汰来说是不够的,但它可能是一个起点。也许序列化模型可以是客户端模型的属性。

关于jquery模板,我通常将jquery模板放在分部视图中。这样,它们就可以轻松地呈现在您的视图中,如下所示:

<script id="SomeTemplate" type="text/x-jquery-tmpl">
    @Html.Partial("Templates/SomeTemplate")
</script>

显然,移植现有应用程序以利用 jquery 模板将需要一些时间和精力。

Have been reading about backbone and knockoutjs myself lately, and was also pondering about how to leverage the framework with asp.net mvc.
One approach to avoid the duplication of models is to serialize (json) the server side viewmodel and use it as your backbone or knockout model. Positive side effect, your client side models already contain the data on page load and don't have to fetch the data via an ajax request when the page is first loaded.
I'm aware that only serializing the serverside viewmodel isn't enough for backbone/knockout, but it could be a starting point. Perhaps the serialized model could be a property on the clientside model.

About jquery templates, I usually put a jquery template in a partial view. This way they are easily rendered in your view like this:

<script id="SomeTemplate" type="text/x-jquery-tmpl">
    @Html.Partial("Templates/SomeTemplate")
</script>

Obviously, porting an existing application to leverage jquery templates will take some time and effort.

も让我眼熟你 2024-12-08 00:30:47

我曾经在一个项目中使用过 Rails 的主干,最后我在 Underscore 中重做了所有模板 - underscore.js 是 Backbone 的依赖项,并带有自己的模板语言。

我的应用程序并不大,所以最终并没有花费很多时间。如果您的应用程序已经相当复杂,那么情况可能会有所不同。

正如 Suhas 所说,Backbone 的集合能够从服务器获取所有模型,并且您可以使用 Jquery 的 ajax 调用的保存功能将它们发回。您所要做的就是确保它们通过 JSON 进行序列化。

我最近发现了一个关于在 ASP.NET MVC 3 中使用 Backbone 的 4 部分系列 - 也许它会对您有所帮助:http://www.bitcandies.com /blog/2011/asp-net-mvc3-restful-application-tutorial-with-backbone-js-part-i/

另外,这为我在尝试 .网MVC:)

I used backbone with Rails on a project once, and I ended up redoing all my templates in Underscore - underscore.js is a dependency of Backbone and comes with its own templating language.

My app was not huge, so it ended up not taking a lot of time. If your app is already pretty complex, than it may be a different story.

Like Suhas said, Backbone's collection has the ability to fetch all your models from the server and you can send them back using a save functionality that piggy-backs on Jquery's ajax calls. All you have to do is make sure they are serialized through JSON.

I found a recent 4-part series on using Backbone with ASP.NET MVC 3 - maybe it will be helpful to you: http://www.bitcandies.com/blog/2011/asp-net-mvc3-restful-application-tutorial-with-backbone-js-part-i/

Plus this makes a good reference for me to refer back to when I make a go at .net mvc :)

微暖i 2024-12-08 00:30:47

我推荐 ddango 在他的回答评论中所说的话。开始开发具有骨干或淘汰的新功能。我们有一个 Web 应用程序,该应用程序的大部分内容都采用传统的 ASP.NET MVC,一旦我们了解了淘汰赛,我们就开始利用淘汰赛创建页面。他们可以和平共处。如果出于某种原因希望更换旧功能(可维护性或您有太多时间;))。然后你可以一次做一块。

我们开始采取的策略是让 ASP.NET MVC 控制器具有两种功能。

  1. 返回数据的操作(由淘汰赛用来填充模板/控件)
  2. 返回 HTML 的操作用于页面请求。

华泰

I recommend what ddango said in his comment to his answer. Start developing new functionality with backbone or knockout. We have a web app that most of the site is in conventional asp.net mvc and once we learned about knockout we started creating pages leveraging knockout. They can coexist peacefully. If then there is a desire to replace old functionality for whatever reason(maintainability or you have too much time on your hands ;) ). Then you can do it a piece at a time.

The strategy we're beginning to take is for asp.net mvc controllers to have two kinds of functions.

  1. Actions that return data (used by knockout to populate templates/controls)
  2. Actions that return HTML are for page requests.

HTH

疯了 2024-12-08 00:30:47

如果您不想将分部重写为模板,则可以通过使用控制器方法来对待它们,这些方法会将分部视图作为 html 返回,您可以在 Backbone 视图的渲染或初始化中检索该部分视图,例如:

        public ActionResult SomeAction(){ 
           var viewModel = new ViewModel(); 
           return View("~/path/to/partial/view.ascx", viewModel); }
        }

您可以采用此方法更进一步,首先在客户端检索视图模型(或构建它),然后将其发布到操作,而不是在操作中创建视图模型。

(我反对这只是迁移到模板时的过渡 - 当您可能已经拥有数据客户端并且只需要模板字符串时,您总是会发布帖子来渲染某些内容)

回复:ViewModels,通过 JsonResult 从控制器加载数据或最初通过序列化是可行的方法。您不需要为 Backbone 模型重复太多代码 - json 会加载到模型中并解析为您可以通过 Backbone api 访问的属性。

绝对推荐这些文章(尽管不是特定于 ASP.NET MVC):

http://backbonetutorials.com/什么-is-a-model/

http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/

cloudedit教程是落后的版本(控制器现在是路由器),但仍然是一个很好的起点 - 而且 Rails 和 ASP.NET MVC 之间的相似之处很容易看出。

但无法说出淘汰赛!

If you would rather not rewrite your partials to templates, you can treat them as such by using controller methods that will return the partial view as html, which you retrieve in render or initialize of the Backbone view, eg:

        public ActionResult SomeAction(){ 
           var viewModel = new ViewModel(); 
           return View("~/path/to/partial/view.ascx", viewModel); }
        }

You could probably take this method a step further by first retrieving the view model on the client side (or building it) and then posting it to the action instead of creating a view model within the action.

(I would argue against this as anything but an interim while you migrate to templates though - you'll always be making a post to render something when you might have the data client side already, and just need the template string)

re: ViewModels, loading data from a controller via JsonResult or by serializing initially is the way to go. You don't need to duplicate much code for Backbone models - the json is loaded into the model and parsed into attributes that you can access via the Backbone api.

definitely recommend these articles (although not asp.net mvc specific):

http://backbonetutorials.com/what-is-a-model/

http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/

The cloudedit tutorial is a version behind (Controllers are now Routers) but is still a good place to start - and the parallels between Rails and ASP.NET MVC are easy to see.

Couldn't speak to knockout though!

夏末染殇 2024-12-08 00:30:47

您可以将 ASP.NET MVC 应用程序用作为视图模型发出 JSON 的 RESTful 服务。 Backbone 模型可以直接映射到此 JSON。

You can use your ASP.NET MVC app as a RESTful service emitting JSON for your view-models. The Backbone models can directly map to this JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文