Bookshelf.js文档的代码示例包含“new”且没有“new”:
这里http://bookshelfjs.org/#Model-instance-hasMany
let Author = bookshelf.Model.extend({
tableName: 'authors',
books: function() {
return this.hasMany(Book);
}
});
// select * from `authors` where id = 1
// select * from `books` where author_id = 1
Author.where({id: 1}).fetch({withRelated: ['books']}).then(function(author) {
console.log(JSON.stringify(author.related('books')));
});
但这里是http://bookshelfjs.org/#Model-instance-fetch
let Book = bookshelf.Model.extend({
tableName: 'books',
editions: function() {
return this.hasMany(Edition);
},
chapters: function{
return this.hasMany(Chapter);
},
genre: function() {
return this.belongsTo(Genre);
}
})
new Book({'ISBN-13': '9780440180296'}).fetch({
withRelated: [
'genre', 'editions',
{ chapters: function(query) { query.orderBy('chapter_number'); }}
]
}).then(function(book) {
console.log(book.related('genre').toJSON());
console.log(book.related('editions').toJSON());
console.log(book.toJSON());
});
那么区别是什么呢?
解决方法:
没有不同.
Model.fetch,Model.query,Model.where和Model.fetchAll是以下的简写:
Model[method] = function(...args) {
return Model.forge()[method](...args);
}
而Model.forge是新手的简写.
Model.forge = function(...args) {
return new this.constructor(...args);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。