Rumah  >  Artikel  >  pembangunan bahagian belakang  >  mysql - 关于backbone+php的destroy问题

mysql - 关于backbone+php的destroy问题

WBOY
WBOYasal
2016-08-04 09:19:54941semak imbas

我通过重写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>
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn