Javascript 余烬-链接到JSON文件
我正在学习codeschool的教程/示例。这一切都很好,但示例使用Javascript 余烬-链接到JSON文件,javascript,json,ember.js,Javascript,Json,Ember.js,我正在学习codeschool的教程/示例。这一切都很好,但示例使用 App.ApplicationAdapter = DS.FixtureAdapter.extend(); 我现在希望保持一切原样,但将产品数据移动到外部JSON文件中 这是我的app.js文件: var App = Ember.Application.create({ LOG_TRANSITIONS: true }); App.Router.map(function(){ this.route('about', {
App.ApplicationAdapter = DS.FixtureAdapter.extend();
我现在希望保持一切原样,但将产品数据移动到外部JSON文件中
这是我的app.js文件:
var App = Ember.Application.create({
LOG_TRANSITIONS: true
});
App.Router.map(function(){
this.route('about', {path:'/aboutus'});
this.resource('products', function() {
this.resource('product', { path: '/:product_id' });
});
});
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.IndexController = Ember.Controller.extend ({
productsCount: 6,
logo: 'images/logo.png',
time: function() {
return (new Date()).toDateString()
}.property()
});
App.Product = DS.Model.extend({
title: DS.attr('string'),
price: DS.attr('number'),
description: DS.attr('string'),
isOnSale: DS.attr('boolean'),
image: DS.attr('string'),
reviews: DS.hasMany('review', {async:true})
});
App.Review = DS.Model.extend ({
text: DS.attr('string'),
reviewedAt: DS.attr('date'),
product: DS.belongsTo('product')
});
App.ProductsRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('product');
}
});
App.Product.FIXTURES = [
{
id: 1,
title: 'Flint',
price: 99,
description: 'Flint is a hard, sedimentary cryptocrystalline form of the mineral quartz, categorized as a variety of chert.',
image: 'images/products/flint.png',
reviews: [100,101]
},
{
id: 2,
title: 'Kindling',
price: 249,
description: 'Easily combustible small sticks or twigs used for starting a fire.',
image: 'images/products/kindling.png',
reviews: [100,101]
}
];
App.Review.FIXTURES = [
{
id: 100,
product: 1,
text: "Sarted a fire in no time"
},
{
id: 101,
product: 1,
text: "Not the brightest flame of the flame"
}
];
这是我的HTML(index.HTML)文件:
然后改变
App.ApplicationAdapter = DS.FixtureAdapter.extend();
致:
等等
我似乎无法链接到此JSON文件。我只是想知道,我是否应该在上面的应用程序适配器中添加其他内容?在HTML文件的头部包含JSON文件对吗
基本上,在制作上面的示例时需要一些帮助,可以使用外部JSON文件
谢谢
更新
我想让这个问题简单一点:
- 我有一个index.html文件、一个app.js文件和一个products.json文件,都在同一个目录中
- 我想在app.js文件中使用此选项:
var App = Ember.Application.create({
LOG_TRANSITIONS: true
});
App.Router.map(function(){
this.route('about', {path:'/aboutus'});
this.resource('products', function() {
this.resource('product', { path: '/:product_id' });
});
});
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.IndexController = Ember.Controller.extend ({
productsCount: 6,
logo: 'images/logo.png',
time: function() {
return (new Date()).toDateString()
}.property()
});
App.Product = DS.Model.extend({
title: DS.attr('string'),
price: DS.attr('number'),
description: DS.attr('string'),
isOnSale: DS.attr('boolean'),
image: DS.attr('string'),
reviews: DS.hasMany('review', {async:true})
});
App.Review = DS.Model.extend ({
text: DS.attr('string'),
reviewedAt: DS.attr('date'),
product: DS.belongsTo('product')
});
App.ProductsRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('product');
}
});
App.Product.FIXTURES = [
{
id: 1,
title: 'Flint',
price: 99,
description: 'Flint is a hard, sedimentary cryptocrystalline form of the mineral quartz, categorized as a variety of chert.',
image: 'images/products/flint.png',
reviews: [100,101]
},
{
id: 2,
title: 'Kindling',
price: 249,
description: 'Easily combustible small sticks or twigs used for starting a fire.',
image: 'images/products/kindling.png',
reviews: [100,101]
}
];
App.Review.FIXTURES = [
{
id: 100,
product: 1,
text: "Sarted a fire in no time"
},
{
id: 101,
product: 1,
text: "Not the brightest flame of the flame"
}
];
App.ApplicationAdapter=DS.RESTAdapter.extend({
xxxxxxxxx
});
在“xxxxxx”中输入什么来加载json文件
谢谢
更新
好吧,我已经弄明白了,哼
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: '/name of directory'
});
在我的例子中,我的项目位于localhost/ember
以及以下工作:
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: '/ember'
});
我也有同样的问题 与从HTML链接到JSON文件不同,您必须通过对文件的请求扩展DS.RESTAdapter,如下所示:
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: '/products.json?jsonp=?'
});
这应该行得通。
让我知道
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: '/name of directory'
});
还要注意,我必须从文件中删除.json扩展名。现在它只是调用产品(一个文本文件)。当我添加.json扩展名时,它找不到文件。Hi,感谢您的回复。我仍然得到一个404错误,没有找到json文件。我的JSON文件名为'products.JSON',与我的'app.js'文件和'index.html'文件位于同一目录中,它与请求语法不兼容?我知道它不能只与扩展一起工作,但与附加的“.jsonp=?”一起工作应该可以。我也让它在没有分机的情况下运行。但是,没有扩展名的文件是不合适的。
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: '/products.json?jsonp=?'
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: '/name of directory'
});