[英]Is it possible for backbone.js to use a json file as a read-only data store?
[英]Backbone.js: Read a json file
我對belimber.js非常陌生,我正在嘗試使用中樞js讀取json文件。 這是我的代碼。
var SaveReportView= Backbone.View.extend({
tagName: "textarea",
el: $('#build'), // el attaches to existing element
events: {
'click #saveReport': 'saveReportHandler'
},
initialize: function(){
this.render();
},
render: function(){
$(this.el).append("<button id='saveReport' class='btn btn-info'>Save</button>");
},
saveReportHandler: function(){
console.log($("#target").html());
console.log($("#table-0").html());
file = fopen(getScriptPath("data.json"), 0);
file_length = flength(file);
content = fread(file, file_length);
console.log(content);
},
});
var reportView= new SaveReportView();
這在控制台日志上給出以下錯誤。
Uncaught ReferenceError: fopen is not defined
任何人都知道我的代碼有什么問題嗎?
謝謝
好吧..您正在用JavaScript文件編寫PHP。 我認為您需要學習一些有關Web開發的基礎知識:)
要在客戶端上使用JS請求json文件,請使用ajax 。 骨干將此數據和請求包裝在一個模型或模型集合中。
您將擁有一個對數據進行提取並使用屬性填充模型的模型。 通常,您要應用於模型的數據是嵌套的,您將覆蓋解析以獲取所需的對象,但是由於您沒有顯示json文件中的內容,因此其基本知識是:
var ReportModel = Backbone.Model.extend({
url: 'data.json'
});
var reportModel = new ReportModel();
reportModel.fetch().done(function() {
console.log('successful');
}).fail(function() {
console.log('failed');
});
典型的方法是在實例化視圖時通過model
選項傳遞實例化的模型。 然后,視圖執行獲取和/或等待對模型的獲取完成this.listenTo(this.model, 'sync', this.onFetchSuccess);
。 如果您迷失了所有這些,我建議您首先從教程中學習一些JS和Backbone基礎知識!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.