首頁  >  文章  >  後端開發  >  mysql - 關於backbone+php的destroy問題

mysql - 關於backbone+php的destroy問題

WBOY
WBOY原創
2016-08-04 09:19:54937瀏覽

我透過重寫backbone sync的destroy()來對應php(點擊刪除按鈕會觸發delete事件),在php裡實現對資料的刪除,但是因為collection裡定義了url,在重寫destroy的url時,無法執行對應操作,求解答!

<code>//Collection
App.Users=Backbone.Collection.extend({
  model : App.User,
  url : function(){
    return '/crud2/save.php';
  },
  initialize : function(){
    this.on('add',function(){
      console.log(this.length);
    });
    this.on('remove',function(){
      console.log(this.length);
    });
  }
});</code>
<code>  `delete : function(){
  //this.model.url='delete.php';
  //console.log(this.model.url);
  $('#Name').val("");
  $('#Phone').val("");
  this.model.destroy({
    url : 'delete.php',
    type : 'POST',
    success : function(){
      console.log('success');
    },
    error : function(){
      console.log('error');
    }
  }
  ); 
}`

</code>
<code>//delete.php
<?php
    date_default_timezone_set('prc');
    error_reporting(E_ALL ^ E_DEPRECATED);
    header("Content-type : application/json; charset = utf-8");  
    $connection=mysql_connect("localhost","root","123456");
    if(!$connection){
        die('connect failed!');
    }else{
        mysql_select_db("backbone");
        mysql_query("set names utf-8");
        $data = json_decode(file_get_contents("php://input"));
        //echo $data;
        file_put_contents("data.txt", $data->username);
        $sql = "delete from user where username == '$data->username'";
        mysql_query($sql);
?></code>

回覆內容:

我透過重寫backbone sync的destroy()來對應php(點擊刪除按鈕會觸發delete事件),在php裡實現對資料的刪除,但是因為collection裡定義了url,在重寫destroy的url時,無法執行對應操作,求解答!

<code>//Collection
App.Users=Backbone.Collection.extend({
  model : App.User,
  url : function(){
    return '/crud2/save.php';
  },
  initialize : function(){
    this.on('add',function(){
      console.log(this.length);
    });
    this.on('remove',function(){
      console.log(this.length);
    });
  }
});</code>
<code>  `delete : function(){
  //this.model.url='delete.php';
  //console.log(this.model.url);
  $('#Name').val("");
  $('#Phone').val("");
  this.model.destroy({
    url : 'delete.php',
    type : 'POST',
    success : function(){
      console.log('success');
    },
    error : function(){
      console.log('error');
    }
  }
  ); 
}`

</code>
<code>//delete.php
<?php
    date_default_timezone_set('prc');
    error_reporting(E_ALL ^ E_DEPRECATED);
    header("Content-type : application/json; charset = utf-8");  
    $connection=mysql_connect("localhost","root","123456");
    if(!$connection){
        die('connect failed!');
    }else{
        mysql_select_db("backbone");
        mysql_query("set names utf-8");
        $data = json_decode(file_get_contents("php://input"));
        //echo $data;
        file_put_contents("data.txt", $data->username);
        $sql = "delete from user where username == '$data->username'";
        mysql_query($sql);
?></code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn