目前我在 ember 中渲染我的 JSON 帖子时遇到了一点问题,我的应用程序的结构在前面有我的 config.ru 文件,这是另一个包含所有 ember 内容的公共文件夹。该应用程序有效,它只是不会渲染帖子,它可以与 sinatra + ember 一起使用,但是自从我吃完葡萄后,我就遇到了麻烦。任何帮助将不胜感激,谢谢。
配置.ru:
require 'sinatra'
require 'JSON'
require 'grape'
require 'sinatra/base'
$posts = {}
$posts[123] = {:title => "Rails is Omakase",
:id => 123,
:_id => 987,
:author => "d2h",
:publishedAt => Date.new(),
:intro => "There are lots of la carte software environments in this world. Places where in order to eat, you must first carefully look over the menu of options to order exactly what you want.",
:extended => "I want this for my ORM, I want that for my template language, and let's finish it off with this routing library. Of course, you're going to have to know what you want, and you'll rarely have your horizon expanded if you always order the same thing, but there it is. It's a very popular way of consuming software.\n\nRails is not that. Rails is omakase."
}
put '*/:id' do
p "entering PUT /*/:id", params
raw = request.env["rack.input"].read ## this is the JSON data as a string
p "raw:", raw
$posts[params[:id].to_i] = JSON.parse(raw)['post'].merge({:id => params[:id].to_i})
p "\n\n+++++ $posts:", $posts
{:id => params[:i]}.to_json
end
class API < Grape::API
default_format :json
get '/posts' do
mount API::posts => '/posts'
data = {:posts => $posts.values}.to_json
return data
end
end
class Web < Sinatra::Base
get '/posts' do
p "entering GET"
data = {:posts => $posts.values}.to_json
p "\n*** data:", data
data
end
get '/' do
File.read(File.join('public', 'index.html'))
end
end
use Rack::Session::Cookie
run Rack::Cascade.new [API, Web]
应用程序.js:
App = Ember.Application.create({});
App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.extend({
url: 'http://localhost:4567'
})
});
App.Router.map(function() {
this.resource('about');
this.resource('posts', function() {
this.resource('post', { path: ':post_id' });
});
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return App.Post.find();
}
});
App.PostController = Ember.ObjectController.extend({
isEditing: false,
edit: function() {
this.set('isEditing', true);
},
doneEditing: function() {
this.set('isEditing', false);
this.get('store').commit();
}
});
var attr = DS.attr;
App.Post = DS.Model.extend({
title: attr('string'),
author: attr('string'),
intro: attr('string'),
extended: attr('string'),
publishedAt: attr('date')
});
var showdown = new Showdown.converter();
Ember.Handlebars.registerBoundHelper('markdown', function(input) {
return new Handlebars.SafeString(showdown.makeHtml(input));
});
Ember.Handlebars.registerBoundHelper('date', function(date) {
return moment(date).fromNow();
});
Github 仓库:https ://github.com/FatOblivion/blogger