-2
我已審查以下文件這表明這種技術與AmpersandViews: https://ampersandjs.com/learn/base-objects-and-mixins/如何在擴展模型和集合的AmpersandJS基礎庫時定義新的派生屬性?
這是一種然而,對於我的工作與模型和集合的,我不能夠通過進一步擴展基本定義任何新的派生屬性模型/集合在我的原生web應用程序中。如果它們是通過fetch()創建的,那麼派生的屬性是'undefined'。但是,如果我在本地創建它們,它們會按預期工作。請參閱下面的代碼段,瞭解我的代碼細目。任何想法,我做錯了什麼?或者,如果這是有意的行爲,是否有任何解決方法? (我包括自骨幹在這個Backbone.js的標籤和與符號的關係如此緊密,使用相同的同步/獲取方法。)
//****************
// app.js (entry page)
//****************
import app from 'ampersand-app'
import Manifest from './manifest'
import Me from './me'
window.app = app
app.extend({
init() {
this.me = new Me({id:123, login:'bob'})
this.me.fetchInitialData()
}
})
app.init()
var m = new Manifest({ _id: 'foo', '@id': 'bar', label: [{ '@value': 'Manifest 1'}] })
document.write(m.app_url) //--> works!
/* After bundled with webpack and initial data is fetched, the following returns undefined in the console:
app.me.presentations.manifests.models[0].app_url
*/
//****************
// me.js
//****************
import Model from 'ampersand-model'
import {Collections} from 'tabula-rasa'
export default Model.extend({
children: {
presentations: Collections
},
fetchInitialData() {
this.presentations.fetch()
}
})
//****************
// collections.js
//****************
import {Collections} from 'tabula-rasa'
import ManifestList from './manifest-collection'
export default Collections.Collection.extend({
collections: {
manifests: ManifestList,
},
derived: {
app_url: {
deps: ['_id'],
fn() {
return 'collections/' + this._id
}
}
}
// manifest-collection.js
import {ManifestList} from 'tabula-rasa'
import Manifest from './manifest'
export default ManifestList.extend({
model: Manifest
})
})
//****************
// manifest.js
//****************
import {Manifest} from 'tabula-rasa'
export default Manifest.extend({
derived: {
app_url: {
deps: ['_id'],
fn() {
return 'manifests/' + this._id
}
}
}
})
(編輯補充代碼到而不是指向外部的GitHub項目。由於npm模塊依賴關係代碼片段不會運行。)
您應該在**問題中添加重現問題**所需的最小代碼(*,不是代碼*的鏈接)...閱讀[mcve] –
啊,@TJ,感謝您的提示...會解釋downvotes。我解決了這個問題。我將用最少的代碼更新問題並列出答案。 – sdellis