我目前正在使用 Backbone 编写应用程序,出于某种原因,它不会更新视图,但仅在某些情况下。
如果我在 index.html#/blog/2 刷新页面,它会很好地加载页面,一切都很好。但是,如果我在 index.html#/blog/1 处刷新页面,然后将 URL 更改为 index.html#/blog/2 并按 Enter(不刷新),则永远不会触发更改。
这是我的路由器:
makeChange: function() {
// Set activePage to the current page_id => /blog/2
var attributes = {activePage: this.page_id};
var $this = this;
// Loop through all sections in the app
app.sections.some( function( section ) {
// Check if section has the page
if( !section.validate( attributes ) )
{
// If it has, set the activePage to /blog/2
section.set( attributes, {silent: true} );
// Set active section to whatever section-id was matched
app.set( {activeSect: section.id}, {silent: true} );
console.log('Calling change!');
// Calling change on both the app and the section
app.change();
section.change();
console.log('Change complete!');
return true;
}
});
}
这是应用程序视图(在上面被称为“应用程序”^):
var AppView = Backbone.View.extend({
initialize: function( option ) {
app.bind( 'change', _.bind( this.changeSect, this ) );
},
changeSect: function() {
var newSect = app.sections.get( app.get('activeSect' ) );
var newSectView = newSect.view;
if( !app.hasChanged( 'activeSect' ) )
newSectView.activate( null, newSect );
else
{
var oldSect = app.sections.get( app.previous( 'activeSect' ) );
var oldSectView = oldSect.view;
newSectView.activate( oldSect, newSect );
oldSectView.deactivate( oldSect, newSect );
}
}
});
如果您需要查看其他类/模型/视图,请告诉我。