Backbone:将静态.json文件从本地驱动器加载到集合

时间:2014-06-17 19:56:56

标签: javascript jquery json backbone.js

来自本地驱动器的静态.json文件是否可以用作Backbone集合中的模型?

我的C-drive中保存了一个文件myjson.json。我无法通过以下设置访问它。

相反,我收到错误XMLHttpRequest cannot load file:///C:/Users/Me/Documents/Test/app/scripts/src/myjson.json. Received an invalid response. Origin 'null' is therefore not allowed access.

是否可以将本地驱动器中的静态文件插入Backbone中的Collection?

我的文件夹结构如下所示

->index.html
->app
    ->scripts
        ->src
            ->main.js
            ->myjson.json
        ->views
            ->app.js
        ->models
            ->ModelAndCollection.js

Inside ModelAndCollection.js:

define(['backbone'], function(Backbone) {

  var TheModel = Backbone.Model.extend({

  });

  var TheCollection = Backbone.Collection.extend({
    model: TheModel,

    url: '../item-data.json'
  });

  return TheCollection;

});

Inside App.js

define(['backbone', '../models/ModelAndCollection'], function(Backbone, ModelAndCollection) {

  var App = Backbone.View.extend({
    initialize: function () {

      this.collection = new ModelAndCollection();

      console.log('initialize!!!');

      this.collection.fetch({
        success: function (data) {
          console.log(data);
        },
        error: function () {
          console.log('error');
        }
      });
    }
  });

  return App;

});

0 个答案:

没有答案