gpt4 book ai didi

javascript - 使用 Backbone.js 和 Underscore.js 的应用程序

转载 作者:行者123 更新时间:2023-11-30 16:58:42 25 4
gpt4 key购买 nike

我是 Backbone 和 Underscore 的初学者。所以这是我制作的 javascript。它即将添加书签并显示它们,这意味着使用来自 Backbone 的模型和 View 。但是我猜我遇到了一个问题,因为运行时什么也没有发生,所以如果有人能指出我的错误??提前致谢。这是 ann.js 之后是 index.html

var app = app || {};
app.Bookmark = Backbone.Model.extend({
defaults: {
key : 'Unknown',
value : 'Example',
lien : 'http://www.example.com'
}
});
app.Ensbookmark = Backbone.Collection.extend({
model : app.Bookmark
});

app.BookmarkView = Backbone.View.extend({
tagName: 'div',
template: $('#bookmarkTemplate').html(),
events :{
'click .delete': 'delBookmark'
},
delBookmark:function(){
this.model.destroy();
this.remove();
},
render: function(){
var tmp1 = _.template(this.template);
this.$el.html(tmp1(this.model.toJSON()));
return this;
/*
this.$el.html(this.template(this.model.attributes));
return this;
*/

}
});

app.EnsbookmarkView = Backbone.View.extend({
el:$( '#bookmarks' ),
initialize: function(initialBookmarks){
this.collection = new app.Ensbookmark(initialBookmarks);
this.render();
this.listenTo(this.collection, 'add', this.renderBookmark);
},
events:{
'click #add':'addBookmark'
},
addBookmark: function (e){
e.preventDefault();
var data = {};
$('#addBookmark div').children('input').each(function(i,el){
if($(el).val()!==""){
data[el.id]=$(el).val();
}
});
this.collection.add(new app.Bookmark(data));
},

render: function() {
this.collection.each(function (item) {
this.renderBookmark(item);
}, this);
},
renderBookmark:function(item){
var BookmarkView = new app.BookmarkView({
model: item
});
this.$el.append(BookmarkView.render().el);
}

});
var appTest = new app();
<!DOCTYPE html>
<html>
<head>
<title>Web</title>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
<script src="ann.js"></script>

</head>

<body>

<script id="bookmarkTemplate" type="text/template">
<ul>
<li><%= key %></li>
<li><%= value %></li>
<li><%= lien %></li>
</ul>
<button class="delete">Supprimer</button>

</script>

<div id="bookmarks">
<form id="addBookmark" action="#">
<div>
Id : <input type="text" id="key"/>
Titre : <input type="text" id="value"/>
Lien : <input type="url" id="lien">

<button id="add">Add</button>
</div>
</form>
</div>
</body>

</html>

最佳答案

改变了 var appTest = new app();

var appTest = new app.EnsbookmarkView;

它可以工作,甚至在控制台中也没有错误

你可以在底部试试

var app = app || {};
app.Bookmark = Backbone.Model.extend({
defaults: {
key : 'Unknown',
value : 'Example',
lien : 'http://www.example.com'
}
});
app.Ensbookmark = Backbone.Collection.extend({
model : app.Bookmark
});

app.BookmarkView = Backbone.View.extend({
tagName: 'div',
template: $('#bookmarkTemplate').html(),
events :{
'click .delete': 'delBookmark'
},
delBookmark:function(){
this.model.destroy();
this.remove();
},
render: function(){
var tmp1 = _.template(this.template);
this.$el.html(tmp1(this.model.toJSON()));
return this;
/*
this.$el.html(this.template(this.model.attributes));
return this;
*/

}
});

app.EnsbookmarkView = Backbone.View.extend({
el:$( '#bookmarks' ),
initialize: function(initialBookmarks){
this.collection = new app.Ensbookmark(initialBookmarks);
this.render();
this.listenTo(this.collection, 'add', this.renderBookmark);
},
events:{
'click #add':'addBookmark'
},
addBookmark: function (e){
e.preventDefault();
var data = {};
$('#addBookmark div').children('input').each(function(i,el){
if($(el).val()!==""){
data[el.id]=$(el).val();
}
});
this.collection.add(new app.Bookmark(data));
},

render: function() {
this.collection.each(function (item) {
this.renderBookmark(item);
}, this);
},
renderBookmark:function(item){
var BookmarkView = new app.BookmarkView({
model: item
});
this.$el.append(BookmarkView.render().el);
}

});
var appTest = new app.EnsbookmarkView;
<!DOCTYPE html>
<html>
<head>
<title>Web</title>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
<script src="ann.js"></script>

</head>

<body>

<script id="bookmarkTemplate" type="text/template">
<ul>
<li><%= key %></li>
<li><%= value %></li>
<li><%= lien %></li>
</ul>
<button class="delete">Supprimer</button>

</script>

<div id="bookmarks">
<form id="addBookmark" action="#">
<div>
Id : <input type="text" id="key"/>
Titre : <input type="text" id="value"/>
Lien : <input type="url" id="lien">

<button id="add">Add</button>
</div>
</form>
</div>
</body>

</html>

关于javascript - 使用 Backbone.js 和 Underscore.js 的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com