1

これは私のログアウトクリックイベントです:

logoutClicked: (event) ->
  event.preventDefault()
  console.log 'userPanel.logoutClicked -> event', event
  console.info App.session
  App.session.destroy
    wait: true

    success: (model, res) ->
      console.log 'session.destroy.success -> model/res', model, res

    error: (model, res) ->
      console.log 'session.destroy.error -> model/res', model, res

これは私のセッションモデルです:

class App.Model.Session extends Backbone.Model

  initialize: ->
    console.log 'Session.init'

  urlRoot: '/session'

これが私のスリムなバックエンドルートです。

$app->delete('/session', function () {
  session_unset();
  exit(true);
});

logoutClickedイベントをトリガーすると、すべて正常に機能していますが、firebugを介したサーバー通信(DELETEまたはGET to / session ...)が表示されません。

Firebugの出力:

userPanel.logoutClicked -> event Object { originalEvent=Event click, type="click", timeStamp=18807379, altri elementi...}
Session { cid="c1", attributes={...}, _changing=false, altri elementi...}
session.destroy.success -> model/res Session { cid="c1", attributes={...}, _changing=false, altri elementi...} null

DELETEリクエストが起動されていないことを確認できます...セッションからnullresを受け取ります。destroysuccesscallback...Backboneを初めて使用しますが、何か提案はありますか?たぶん私はBackbone.syncをセットアップする必要がありますか?

4

2 に答える 2

5

新しいモデル(Model.isNew()=== true)でModel.destroy(...)を呼び出す場合。.destroy(...)は何もしません。

参照:

One way to "force" destroy to be called is to manually set Model's ID to be not NULL, like...

App.Session.set('id', '_session_id_');

However, when you retrieve App.Session, its ID should already be set to the session ID. In that case, when retrieving a session from the server, you may want to do this step manually.


Side Note

If you want to call your ID attribute as sessionID instead of just id, you can also override the default idAttribute on Backbone.Model.

App.Session.Model
  idAttribute: 'sessionId'
于 2013-02-12T13:12:47.483 に答える
2

does App.session have a ID?

only model with a id attribute can be destroy.

in other words, you need to save first.

于 2013-02-12T13:22:51.453 に答える