使用Ember前端渲染JSON帖子的问题,Sinatra使用Grape后端

时间:2014-04-14 15:39:03

标签: json ember.js sinatra grape

目前我在使用ember渲染我的JSON帖子时遇到了一些麻烦,我的应用程序的结构在前面有我的config.ru文件,另一个公共文件夹里面有所有的ember内容。该应用程序工作,它只是不会渲染帖子,它与sinatra +余烬一起工作,但因为我绞葡萄我一直遇到麻烦。非常感谢任何帮助。

Config.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]

app.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

0 个答案:

没有答案