简体   繁体   English

如何刷新骨干应用程序中的页面

[英]How to refresh a page in a backbone application

I am using backbone to build my web app. 我正在使用骨干来构建我的网络应用程序。

Currently I am facing an issue whereby if I am on the home page, I am unable to refresh the same page by just clicking on the 'home' button again. 目前我正面临一个问题,即如果我在主页上,我再也无法点击“主页”按钮刷新同一页面。

I believe that this is the limitation provided by backbone (does not reload the page if the same URL is called) 我相信这是骨干提供的限制(如果调用相同的URL,则不会重新加载页面)

在此输入图像描述

Is there any way around this? 有没有办法解决? So that I can trigger a page reload when I click on the home button again ie call the same URL again? 因此,当我再次单击主页按钮时,我可以触发页面重新加载,即再次调用相同的URL?

You're looking for Backbone.history.loadUrl . 您正在寻找Backbone.history.loadUrl From the Annotated Source : 来自注释来源

Attempt to load the current URL fragment. 尝试加载当前的URL片段。 If a route succeeds with a match, returns true . 如果路由成功匹配,则返回true If no defined routes matches the fragment, returns false . 如果没有定义的路由与片段匹配,则返回false

So, for a simple refresh link, you can add the following event to your Backbone.View : 因此,对于简单的刷新链接,您可以将以下事件添加到Backbone.View

events: {
  'click a#refresh': function() {
    Backbone.history.loadUrl();
    return false;
  }
}

Looking at the backbone.js source, it seems as though this is not possible by default, since it only responds to a url change. 看看backbone.js源代码,默认情况下这似乎是不可能的,因为它只响应了一个url更改。 And since clicking the same link, you would not trigger a change event. 并且由于单击相同的链接,您不会触发更改事件。

Code in Backbone.History: Backbone.History中的代码:

$(window).bind('hashchange', this.checkUrl);

You'll need to handle a non-change yourself. 您需要自己处理不变更。 Here's what I did: 这是我做的:

$('.nav-links').click(function(e) {
    var newFragment = Backbone.history.getFragment($(this).attr('href'));
    if (Backbone.history.fragment == newFragment) {
        // need to null out Backbone.history.fragement because 
        // navigate method will ignore when it is the same as newFragment
        Backbone.history.fragment = null;
        Backbone.history.navigate(newFragment, true);
    }
});

In case you want to reload the whole page with a desired view. 如果您想要使用所需视图重新加载整个页面。 This might make senes for the home button. 这可能会使主页按钮变为senes。 The advantage will be that it'll refresh the memory. 它的优点是它会刷新内存。

Go to any route without loading it (without the 'true') and then reload 转到任何路线而不加载它(没有'true')然后重新加载

Backbone.history.navigate('route/goes/here');
window.location.reload();

The backbone.history.loadUrl is the solution. backbone.history.loadUrl是解决方案。

The click handling function of your Home button (if the home is the root / of your WebApp) should be: 主页按钮的单击处理功能(如果主页是WebApp的根目录)应该是:

myAppRouter.navigate('');
Backbone.history.loadUrl();

I needed to 'refresh' my page on orientation change event because not all of my css media queries for portrait mode where executed correctly by default. 我需要在方向更改事件上“刷新”我的页面,因为并非我的所有css媒体查询都是默认情况下正确执行的纵向模式。 This is what I ended up doing: 这就是我最终做的事情:

Backbone.history.fragment = null;
Backbone.history.navigate(document.location.hash, true); 

Of course, for the completeness of this answer, this was wrapped in some orientation change handling code: 当然,为了完成这个答案,这包含在一些方向更改处理代码中:

window.addEventListener('orientationchange', function () {
    Backbone.history.fragment = null;
    Backbone.history.navigate(document.location.hash, true); 
}, false);

You could also just make a meaningless change to the url by adding a random number to the end: 你也可以通过在末尾添加一个随机数来对网址进行无意义的更改:

var url = $(this).attr('href') + '?rand=' + Math.floor(Math.random() * 1000);
Backbone.history.navigate(url, true);

This is especially useful for IE since it will not request new data via an AJAX call if the url has not changed. 这对IE尤其有用,因为如果url没有改变,它将不会通过AJAX调用请求新数据。

You can easily refresh the page using below approach: 您可以使用以下方法轻松刷新页面:

@win_loc = window.location.toString().replace("#","")
Backbone.history.redirectTo(@win_loc)

You can often modify your route to include a splat, so that you can append a random string to the end of an href to make it unique, but still match the "home" route. 您通常可以修改路线以包含splat,以便您可以将随机字符串附加到href的末尾以使其唯一,但仍然匹配“home”路径。 (Note: make this your last route.) (注意:这是你的最后一条路线。)

In the routes : 在路线

routes: {
    "*str": "home"
}

In the view : 在视图中

render: function() {
    var data = {href: +(new Date())};
    _.extend(data, this.model.attributes);
    this.$el.html(this.template(data));
    return this;
}

In the template (handlebars shown here): 在模板中 (此处显示的把手):

<a href="{{href}}">Home Link</a>

In order to provide a reusable snippet of code I augmented the Backbone.View with a helper method to reuse it wherever we need to reload the same page despite the Backbone router limitations. 为了提供可重复使用的代码片段,我使用辅助方法扩充了Backbone.View,以便在我们需要重新加载同一页面的地方重用它,尽管Backbone路由器有限制。

In our app.js file or any other proper place 在我们的app.js文件或任何其他适当的地方

Backbone.View = Backbone.View.extend({
    refreshPage: function(e){
        if(e.target.pathname){
            Backbone.history.navigate(e.target.pathname);
        }
        window.location.reload();           
    }
});

This way if we have a link such as 这样,如果我们有一个链接,如

<a href="/whatever" id="whateverId">Some text </a>

In our View we would just need to bind the click event to that callback to leverage the refresh page feature, as far as the refreshPage helper will be available through the prototype chain 在我们的视图中,我们只需要将click事件绑定到该回调以利用刷新页面功能,只要通过原型链可以获得refreshPage助手即可

events: {
    'click #whateverId': 'refreshPage', 
}

As far as we don't need to change the "a tag" it's a cleaner solution that will save some boilerplate coding 只要我们不需要更改“标签”,它就是一个更清洁的解决方案,可以节省一些样板编码

Hope it helps 希望能帮助到你

它适用于

 myBackboneRouter.navigate("users", {trigger: true})

Here's my favorite approach so far: 到目前为止,这是我最喜欢的方法:

 if (!Backbone.history.navigate(urlPath, true)) {
      Backbone.history.loadUrl();
 }

This is not upto backbone. 这不是主干。 The same works fine in chrome(webkit browsers), but not in firefox. 同样适用于chrome(webkit浏览器),但不适用于firefox。 Its a browser behavior 它是一种浏览器行为

The difference between http://localhost.com/ and http://localhost.com/#!/ is that the second is a link to anchor, it does not load a page, it only looks for the anchor in the current page, and scrolls to it if found. http://localhost.com/http://localhost.com/#!/之间的区别在于第二个是锚点链接,它不加载页面,它只查找当前页面中的锚点,如果找到则滚动到它。 You need to make your link looks like the first one, no "#" close to end. 您需要使链接看起来像第一个,没有“#”接近结束。

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

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