0

本のリストを管理する単純な Web アプリケーションのバックエンドとして CherryPy を使用し、フロントエンドとして emberjs を使用することを検討しています。Cherrypy は、インデックスのリクエスト時にハンドルバー テンプレートを提供するだけです。

import os
import cherrypy
from google.appengine.api import users
from google.appengine.ext import ndb

class Root:

    def __init__(self):
        # book REST API
        self.books = BookAPI()

    @cherrypy.expose
    def index(self):
        return open(os.path.join(template_env, 'index.hbs'))

また、クラス BooksAPI と Books を使用して、書籍オブジェクトを格納するために Google データ ストレージを使用する RESTfull API として機能します (現在は isbn のみを格納しています)。

class BookAPI():
    exposed=True

    @cherrypy.tools.json_out()
    def GET(self, isbn=None):

        # get the current user
        user = users.get_current_user()

        if(isbn is None):

            # query all books current user
            ancestor_key = ndb.Key("Library", str(user.user_id()))
            books = Book.query_books(ancestor_key).fetch(20)

            # convert to JSON list of books
            book_list = []
            for index, b in enumerate(books):
                book_list.append({'id': index, 'isbn': b.isbn})
            result = {
                "books": book_list
            }

        return result

    def POST(self, isbn):

        # get the current user
        user = users.get_current_user()

        # create book and save in data storage
        parent_key = ndb.Key('Library', user.user_id())
        book = Book(parent=parent_key, isbn=isbn)
        book.put()

    ...

class Book(ndb.Model):

    isbn = ndb.StringProperty()

    @classmethod
    def query_books(cls, ancestor_key):
        return cls.query(ancestor=ancestor_key)

emberjs クライアント側では、RESTAdapter を使用します。

window.Books = Ember.Application.create();
Books.ApplicationAdapter = DS.RESTAdapter.extend();

私のemberjsブックモデルは次のように定義されています:

Books.Book = DS.Model.extend({
  isbn: DS.attr('string'),
});

そして、次のブック コントローラーを追加しました。

Books.BookController = Ember.ObjectController.extend({
  actions: {
    removeBook: function() {
      var book = this.get('model');
      book.deleteRecord();
      book.save();
    }
  }
});

Books.BooksController = Ember.ArrayController.extend({
  actions: {
    createBook: function() {
      // get book isbn
      var isbn = this.get('newIsbn');
      if(!isbn.trim()) { return; }
      // create new book model
      var book = this.store.createRecord('book', {
        isbn: isbn,
      });
      // clear the 'new book' text field
      this.set('newIsbn', '');
      // Save model
      book.save();
    }
  }
});

そして最後に、次のルート:

Books.Router.map(function () {
  this.resource('books', { path: '/' });
});

Books.BooksRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('book');
  }
});

FixedAdapter を使用して本の追加と削除が機能した後、RESTAdapter に切り替えました。

GET メソッドが機能しました。Emberjs は自動的に GET メソッドを呼び出し、index.hbs テンプレートに表示される JSON 形式の本のリストを正常に取得します。

しかし、emberjs は予期しない方法で POST メソッドを呼び出します。isbn を POST データとして追加せずに、ember が空の POST を送信しているようです。チェリーピー POST 関数から isbn キーワード引数を削除すると、関数が呼び出されるためです。ただし、book オブジェクトを作成するには isbn が必要です。

ここで明らかなことを忘れている可能性がありますが、何がわかりません。私が何を忘れているか、間違っているかを誰かが知っていますか? ありがとう。

バスティアン

4

1 に答える 1