简体   繁体   English

如何使用Backbone.js从索引中提取新请求?

[英]How can I pull a new request from my index with Backbone.js?

I have an index that creates randomly generated dynamic content. 我有一个创建随机生成的动态内容的索引。

So everytime you load the index, it'll create a series of view that are dependent on what my Rails model has produced and sent to Backbone. 因此,每次加载索引时,它都会创建一系列视图,这些视图取决于我的Rails模型产生并发送给Backbone的视图。

From backbone, I am curious what I could do to "refresh" the page without doing something like this : 从骨干开始,我很好奇我可以做的“刷新”页面而无需执行以下操作:

window.location = '/'

I'd like to do it within Backbone.. something like this : 我想在Backbone内做。

Backbone.history.navigate('/', {trigger: true, replace: true});

But this doesn't necessarily send a new request to the url. 但这不一定会将新请求发送到url。

All I would need to do to accomplish my goals is send a GET request to / , which should return a JSON object I can pipe through the rest of my Backbone app. 实现目标所需要做的就是向/发送GET请求,该请求应该返回一个JSON对象,我可以通过该对象通过Backbone应用程序的其余部分进行传递。

Is there a way to send this request within Backbone? 有没有办法在Backbone中发送此请求? Or should I just go a traditional jQuery route, and just make a $.get request? 还是我应该走传统的jQuery路线,然后提出$.get请求?

Since your REST api returns a JSON object, simply use a Backbone.Model to structure this data. 由于您的REST api返回JSON对象,因此只需使用Backbone.Model来构造此数据。 You can then bind events to do whatever you like in your application. 然后,您可以绑定事件以在应用程序中执行任何操作。

var RandomData = Backbone.Model.extend({ url: '/' });

var randomData = new RandomData();

// Here, `Backbone` can be substituted by any `View`, `Collection`, `Model...
Backbone.listenTo( randomData, 'change', function() {
  //Do something everytime this changes.
});

// When you need to issue a GET '/' request. The following will put the 
// JSON response inside of `randomData.attributes`
randomData.fetch();

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

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