2016-03-23 1 views
0

data配列のオブジェクトとmark_readメソッドがあり、これはPUTリクエストを私のRailsアプリケーションに送信します。それはのdataのオプションはオブジェクトインスタンスではないように見えますが、どうすれば修正できますか?このコンテキストのajaxデータオプション

Notifications.prototype = { 
    constructor: Notifications, 
    ... 
    mark_read: function() { 
    $.ajax({ 
     method: "PUT", 
     url: '/notifications/mark_read', 
     data: this.data.slice(0,5) 
    }); 
    } 
    ... 
} 
+0

あなたは何を期待していますか?データはどこに定義されていますか? – matmo

答えて

0

$ .ajax関数の内部からアクセスする前に、 "this"をクロージャに格納する必要があります。

それはthisのスコープがmark_read機能です。この

Notifications.prototype = { 
    constructor: Notifications, 
    ... 
    mark_read: function() { 
     var me = this; 
     $.ajax({ 
      method: "PUT", 
      url: '/notifications/mark_read', 
      data: me.data.slice(0,5) 
     }); 
    } 
    ... 
} 
0

のようなものである必要があり、そうthisのためのコンテキストが時に呼ばれているものは何でもNotificationオブジェクトmark_read()です。

関連する問題