小编tra*_*ss0的帖子

使用同步ajax调用有什么缺点?

这个问题肯定可以应用于jQuery,但在这种情况下,我指的是Prototype.在原型文档中,它说,

由于同步使用相当令人不安,并且通常味道不好,因此您应该避免更改此设置.认真.

我不确定使用同步ajax调用有什么缺点.似乎有许多实例必须等待调用返回(不使用特定的回调函数).例如,我目前使用Prototype onSuccess, onFailure and onComplete来处理其余的代码.

但是,我使用的Web服务(所有内部)跨越大多数项目,我的任务是创建更多可重用的代码.一个示例是返回客户属性的客户类.一个简单的例子(请记住,我只显示基本功能以保持简单):

Customer = Class.create({ 

    initialize: function(customerId) { 

        new Ajax.Request('some-url', {
            method: 'get',
            parameters: {
                customerId: customerId
            },
            onSuccess: this.setCustomerInfo.bind(this)
        }

    },

    setCustomerInfo: function(response) {

        //for the sake of this example I will leave out the JSON validation

        this.customerInfo = response.responseText.evalJSON();

    } 

});
Run Code Online (Sandbox Code Playgroud)

因此,使用这个简单的类我可以在任何项目中执行以下操作来获取客户信息.

var customer = new Customer(1);

//now I have the customer info
document.write(customer.customerInfo.firstName);
Run Code Online (Sandbox Code Playgroud)

使用上面的代码不会打印出客户的名字.这是因为ajax调用是异步的.它将执行document.writeWeb服务是否带回客户数据.但是,在数据恢复并设置了客户变量之前,我不想做任何事情.为了解决这个问题,我将ajax调用设置为synchronous,这样浏览器就不会继续运行直到new Customer(1);完成.

这种方法似乎有效(将异步设置为false)但是阅读Prototype文档会让我停下来.使用这种方法有什么缺点?有没有其他方法可以做到,效率更高等等?

我将不胜感激任何反馈.

ajax jquery class prototypejs

6
推荐指数
3
解决办法
2720
查看次数

蛋糕PHP模型关联 - 我做错了什么?

无论我阅读多少次文档,看看我无法让我的模型与另一个模型"关联"的例子.

这是我的例子:

我有一张名为工具包的表和一张名为成绩的表(读书和作者).

套件看起来像这样(简化):

  1. id(PK)
  2. grade_id(FK)
  3. 名称

成绩看起来像这样:

  1. id(PK)

每个套件只有1个等级.我没有看到命名约定的任何问题,它遵循API中的规定.

-

我有一个名为KitsController.php的控制器,如下所示:

class KitsController extends AppController {
    public function index() {
        //simple find that limits to one (for readability)
        $pigs = $this->Kit->find("all", array( 
            "limit" => 1
        ));

        $this->set("pigs", $pigs);
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个相应的index.ctp文件,输出变量$ pigs(不是我知道的生产级变量名).

-

我的模型名为KitModel.php

class Kit extends AppModel {
    public $belongsTo = "Grade";//here is where I am confused
}
Run Code Online (Sandbox Code Playgroud)

...和占位符的空级别模型

class Grade extends AppModel {

}
Run Code Online (Sandbox Code Playgroud)

-

问题:

当数组在页面上打印出来时,它看起来像这样.

Array ( [0] …
Run Code Online (Sandbox Code Playgroud)

cakephp model-associations

0
推荐指数
1
解决办法
111
查看次数

标签 统计

ajax ×1

cakephp ×1

class ×1

jquery ×1

model-associations ×1

prototypejs ×1