這是 router 中的一個函數(shù)。routes: { "post/:id": "postEdit"},postEdit: function (id) { console.log('router.js, got router: #/post/' + id);
UILoading($("#main")); var model = new PostModel({_id: id});
model.fetch({ success: function () { new PostEditView({model: model});
}, error: function () { console.log("failed, on router: #/post/" + model.id);
}
});
}接下來是 View 的代碼var PostEditView = Backbone.View.extend({
el: '#main',
template: doT.template(PostEditTemplate),
events: { 'click #savePost': 'save'
},
initialize: function () {
_.bindAll(this, 'render'); this.model.bind("change", this.render, this); var converter = Markdown.getSanitizingConverter(); this.editor = new Markdown.Editor(converter); this.render();
},
render: function () { this.$el.html(this.template(this.model.toJSON())); this.editor.run();
},
save: function () { this.model.set({
title: $("#post_title").val(),
slug: $("#post_slug").val(),
created: $("#post_created").val(),
tags: $("#post_tags").val().split(','),
content: $(".post_content").val()
}); this.model.save();
}
});最后發(fā)現(xiàn),當(dāng)訪問過多次#/post/5103fbb3817feb1c10000001,/#/post/5103c114ce4c724c12000002 后,save 這個函數(shù)會重復(fù)調(diào)用。
相當(dāng)于之前的 model 沒有被釋放,事件重復(fù)執(zhí)行了。 如何解決這個問題呢?
慕的地8271018
2023-05-02 15:10:01