BackboneJs:如何在页面标记中引导我的数据,以及何时将它们分配给我的集合

发布于 2024-12-12 02:13:24 字数 889 浏览 0 评论 0原文

所以, 构建一个使用多个(现在是 2 个)全局集合的应用程序, 它是文档和患者的目录,它们之间存在关系,但不像 1 个文档或属于 1 个患者的文档列表那样,所以它们实际上是 2 个独立的集合,

我的应用程序的结构非常相似的模块系统这里是如何描述的: http://weblog.bocoup.com/organizing-your-backbone -js-application-with-modules

backbone.js 文档提到了引导,要

<script>
  Accounts.reset(<%= @accounts.to_json %>);
</script>

在 Rails 应用程序中执行类似的操作,但是我需要在 asp.net MVC3 中以不同的方式执行此操作,很可能我只会打印出我的 json 字符串,而无需 <%= %>这不是剃刀视图引擎风格),

但我的问题是,

这个 Accounts.reset(...data...); 只是浮动在我的标记中的某个位置,它不是以任何方式我的模块系统结构很好,有没有办法很好地做到这一点? 我可以从模块中的哪里获取数据?

还有另一个附带问题,假设我的主干应用程序中有一条路线 http://example.com/#documents

有人直接调用此链接,在执行路线本身之前,我的应用程序是否会按时准备好数据(来自引导程序)?

So,
building an application that uses multiple (2 for now) global collections,
it is a catalog of both documents and patients, they have relations, but not as in, 1 document or a list of documents belonging to 1 patient, so they are in fact 2 separate collections,

my app is structured in a module system very similar to how it is described here:
http://weblog.bocoup.com/organizing-your-backbone-js-application-with-modules

the backbone.js documentation says about bootstrapping, to do something like this,

<script>
  Accounts.reset(<%= @accounts.to_json %>);
</script>

that is within a Rails application, i would however need to do it differently in asp.net MVC3, most likely i would just print out my json string without the <%= %> which isn't razor view engine style)

but my question here is,

this Accounts.reset(...data...); is just floating somewhere in my markup, it is not in any way nicely structured in my module system, isn't there a way to nicely do this?
where as i can get the data, from within my module?

and another side question, suppose i have a route in my backbone app http://example.com/#documents

and someone calls this link directly, will my app have the data ready (from the bootstrap) on time, before the route itself is executed?

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

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

发布评论

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

评论(3

萌面超妹 2024-12-19 02:13:24

我倾向于设置 application 对象 - 一个封装启动应用程序所需的所有代码的对象。然后,我将参数传入该对象的构造函数,以便应用程序对象可以使用它们。将预加载的 JSON 数据传递到应用程序对象中是我所做的事情之一,以保持代码的合理性和封装性。

通常情况是这样的:

MyApp = (function(Backbone, _){
  var MyModel = Backbone.Model.extend({});

  var MyCollection = Backbone.Collection.extend({
    model: MyModel
  });

  var MyView = Backbone.View.extend({
    initialize: function(){
      this.collection.bind("reset", this.render, this);
    },

    render: function(){
      // do stuff with the collection here
    }
  });

  var myApp = function(initialModels){
    this.start = function(){
      this.models = new MyCollection();
      this.myView = new MyView({collection: this.models});
      this.models.reset(initialModels);  
    };
  };

  return myApp;
})(Backbone, _);

然后在需要启动应用程序的页面中,我这样做:

<script language="javascript">
  var myApp = new MyApp(<%= mymodels.to_json %>);
  myApp.start();
</script>

这当然是 Rails 版本。只需将 <%= ... %> 替换为 ASP.NET MVC 中 Razor 语法的版本即可。

I tend to setup application objects - an object that encapsulates all of the code needed to start my application. I then take parameters into that object's constructor, so that they can be used by the app object. Passing pre-loaded JSON data into the app object is one of the things I do, to keep the code sane and encapsulated.

It goes something like this, usually:

MyApp = (function(Backbone, _){
  var MyModel = Backbone.Model.extend({});

  var MyCollection = Backbone.Collection.extend({
    model: MyModel
  });

  var MyView = Backbone.View.extend({
    initialize: function(){
      this.collection.bind("reset", this.render, this);
    },

    render: function(){
      // do stuff with the collection here
    }
  });

  var myApp = function(initialModels){
    this.start = function(){
      this.models = new MyCollection();
      this.myView = new MyView({collection: this.models});
      this.models.reset(initialModels);  
    };
  };

  return myApp;
})(Backbone, _);

And then in my page that needs to start up the app, I do this:

<script language="javascript">
  var myApp = new MyApp(<%= mymodels.to_json %>);
  myApp.start();
</script>

That's the rails version of course. Just replace the <%= ... %> with your version for the Razor syntax in ASP.NET MVC.

还不是爱你 2024-12-19 02:13:24

如果在 Rails 上:除了 Dericks 答案之外,您可能还想使用 Gon 来“将 Rails 变量获取到js”。

然后你会像这样初始化你的应用程序:

<script language="javascript">
  var myApp = new MyApp(gon.mymodels);
  myApp.start();
</script>

if on Rails: in addition to Dericks answer you might want to use Gon to "get your Rails variables in your js".

then you would initialize your app like that:

<script language="javascript">
  var myApp = new MyApp(gon.mymodels);
  myApp.start();
</script>
冷血 2024-12-19 02:13:24

德里克就是一个很好的例子!对于 MVC 3+ 使用以下语法:

<script language="javascript">
  var myApp = new MyApp( @Html.Raw(Json.Encode(Model)) );
  myApp.start();
</script>

The excellent example from Derick! For MVC 3+ use this syntax:

<script language="javascript">
  var myApp = new MyApp( @Html.Raw(Json.Encode(Model)) );
  myApp.start();
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文