YUI3数据源返回自定义对象

发布于 2024-12-07 11:32:23 字数 1101 浏览 8 评论 0原文

如何将 YUI3 sendRequest 应用于数据源以返回预定义对象,而不是普通对象?

例如,我有这个基类及其方法:

function Student(id, name){
   this.id = id;
   this.name = name;
}
Context.prototype.setId   = function(id){ this.id = id; };
Context.prototype.setName = function(name){ this.name = name; };
Context.prototype.getId   = function(){ return this.id; };
Context.prototype.getName = function(){ return this.name; };

我有这段代码,用于从 API 检索数据,对其进行规范化并将数据作为对象返回:

var studApiDataSource = new Y.DataSource.Get({source: API_URL});

studApiDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
  schema: {
    resultListLocator: "response.student",
    resultFields: ["id","name"]
  }
});

var myCallback = function(e) {
  Y.Array.each(e.response.results, function(stud){
    Y.log(stud.id+' '+stud.name);
  }
}

studApiDataSource.sendRequest({
  request: "?cmd=getStudents",
  callback: {
    success: myCallback,
    failure: function (e) { }
  }
});

由studApiDataSource.sendRequest() 检索并传递给 myCallback 的对象数组是普通对象,具有 id 和 name 属性。但是,我希望这些是 Student 对象,也具有它们的成员函数(getId、getName 等)

How can I get the YUI3 sendRequest applied to a Datasource to return predefined objects, instead of plain ones?

For example, I have this Base class with its methods:

function Student(id, name){
   this.id = id;
   this.name = name;
}
Context.prototype.setId   = function(id){ this.id = id; };
Context.prototype.setName = function(name){ this.name = name; };
Context.prototype.getId   = function(){ return this.id; };
Context.prototype.getName = function(){ return this.name; };

And I have this code that retrieves data from an API, normalizes it and returns data as objects:

var studApiDataSource = new Y.DataSource.Get({source: API_URL});

studApiDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
  schema: {
    resultListLocator: "response.student",
    resultFields: ["id","name"]
  }
});

var myCallback = function(e) {
  Y.Array.each(e.response.results, function(stud){
    Y.log(stud.id+' '+stud.name);
  }
}

studApiDataSource.sendRequest({
  request: "?cmd=getStudents",
  callback: {
    success: myCallback,
    failure: function (e) { }
  }
});

The array of objects retrieved by studApiDataSource.sendRequest() and passed to myCallback are normal objects, with id and name properties. However, I want these to be Student objects, with their member functions too (getId, getName etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-12-14 11:32:23

我不确定我完全理解,但你可以做如下的事情。

var studentJSON = "{\"id\": 17, \"name\":\"my name is\"}";
function Student(obj){
  this.name = obj.name;
  this.id = obj.id;
}
Student.prototype.setId   = function(id){ this.id = id; };
Student.prototype.setName = function(name){ this.name = name; };
Student.prototype.getId   = function(){ return this.id; };
Student.prototype.getName = function(){ return this.name; };

YUI().use('json-parse', 'json-stringify', function (Y) {

    try {

        var stud = new Student(Y.JSON.parse(studentJSON));
        alert(stud.getId());
    }
    catch (e) {
        alert(e);
    }

});

I'm not sure I fully understand, but you could do something like the following.

var studentJSON = "{\"id\": 17, \"name\":\"my name is\"}";
function Student(obj){
  this.name = obj.name;
  this.id = obj.id;
}
Student.prototype.setId   = function(id){ this.id = id; };
Student.prototype.setName = function(name){ this.name = name; };
Student.prototype.getId   = function(){ return this.id; };
Student.prototype.getName = function(){ return this.name; };

YUI().use('json-parse', 'json-stringify', function (Y) {

    try {

        var stud = new Student(Y.JSON.parse(studentJSON));
        alert(stud.getId());
    }
    catch (e) {
        alert(e);
    }

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文