我开始了一个新的BackboneJS项目,我实现了一些简单的代码。
它没有调用正确的函数,我想,我没有看到任何错误,我也没有看到预期的console.log
调用。
import _ from 'underscore';
import {Events} from 'backbone';
import Observables from './Models/Observables';
import ObservablesView from './Views/ObservablesView';
(function () {
let setGlobals = function () {
window.App = {};
App.events = _.clone(Events);
};
let init = function () {
setGlobals();
// Get the collection from localStorage.
let observables = new Observables();
new ObservablesView({el: "#observables", collection: observables});
Backbone.history.start({pushState: true});
};
window.addEventListener('load', init);
})();
这是我的路由器:
import {Backbone} from 'backbone';
import {Router} from 'backbone';
const FedRouter = Router.extend({
routes: {
'': 'index'
},
index: function () {
App.events.trigger('observables');
}
});
export default FedRouter;
这是我的观点:
import {View} from 'backbone';
import FedRouter from "../FedRouter";
/**
* ObservablesView is the list of items we want to keep track of.
*/
const ObservablesView = View.extend({
router: null,
/**
* Init a new view object.
* @constructor
*/
initialize: function () {
this.router = new FedRouter();
App.events.on('observables', this.loadObservables, this);
},
loadObservables: function () {
console.log("loadObservables");
this.collection.fetch({
success: (collection) => console.log(collection),
error: (collection, response) => console.log(response)
});
}
});
export default ObservablesView;
我的收藏:
import {Backbone} from 'backbone';
import {Collection} from 'backbone';
import Observable from './Observable';
import Store from 'backbone.localstorage';
const Observables = Collection.extend({
model: Observable,
localStorage: new Store('fed-observables')
});
export default Observables;
最后我的模特:
import {Model} from 'backbone';
const Observable = Model.extend({
});
export default Observable;
那么为什么我没有看到记录的“loadObservables”消息。我没有正确设置什么?