使用骨干模型从 JSON 检索特定数据

发布于 2024-11-28 06:11:15 字数 1019 浏览 1 评论 0原文

我正在创建应用程序的客户端视图,并且需要帮助从 JSON 文件中检索特定数据。我使用 Backbone.js 和 Underscore.js 来实现这一点。

(function($) {

      window.Node = Backbone.Model.extend({
         getName:    function(){
             return this.get('Name');
         } 
     });

      window.Nodes = Backbone.Collection.extend({
         model:Node,
          url: '/packageview.json'
    });

  window.NodeView = Backbone.View.extend({

    tagName: "div",

    className: "package-template",

    events:{

      "click #display-name"       :    "displayname",         
    },

    //.. I have a render and initialize function here which should not be a concern


    displayname: function(){
      var node = new Node();
      alert(node.getName());  //trying to alert
    },

  });
});

我正在尝试从模型中获取名称并提醒它。我的 html 中有一个带有 id 的按钮,当我按下该按钮时,我会收到“未定义”警报。我的 JSON 文件如下所示:

{
  "Id": 2,
  "Name": "Some Package",
  "IsComplete": false,
  "IsNodeTagComplete": false
}

我认为我在某个地方犯了一个愚蠢的错误。我对模型的期望太高了吗?

I am creating a client view of an application and I need help with retrieving specific data from my JSON file. I am using Backbone.js along with Underscore.js to achieve this.

(function($) {

      window.Node = Backbone.Model.extend({
         getName:    function(){
             return this.get('Name');
         } 
     });

      window.Nodes = Backbone.Collection.extend({
         model:Node,
          url: '/packageview.json'
    });

  window.NodeView = Backbone.View.extend({

    tagName: "div",

    className: "package-template",

    events:{

      "click #display-name"       :    "displayname",         
    },

    //.. I have a render and initialize function here which should not be a concern


    displayname: function(){
      var node = new Node();
      alert(node.getName());  //trying to alert
    },

  });
});

I am trying to get the name from model and alert it. I have a button in my html with an id, and when I press that button I get "undefined" as an alert. Here is how my JSON file looks:

{
  "Id": 2,
  "Name": "Some Package",
  "IsComplete": false,
  "IsNodeTagComplete": false
}

I think I am making a silly mistake somewhere. Am I expecting way to much from model?

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

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

发布评论

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

评论(2

残龙傲雪 2024-12-05 06:11:15

我在这里做的是这个

 window.jsonAccess = Node.extend({ // Here Node is my above mentioned model

    getJSON: function(){
        var collection = nodeInstance.toJSON(); // nodeInstance is an instance of my collection Nodes
        return collection; //returns JSON
    }
});
jAccess = new jsonAccess();

所以这就是我正在做的访问 JSON

 getNodeId: function(){ //Function to get Node Id from JSON
        objectJSON = jAccess.getJSON(); // Get JSON 
        _.each(objectJSON, function(action){
            _.each(action.Nodes, function(action){

这解决了我的目的,但不完全是在主干中使用 getter 的方式。

What I am doing here is this

 window.jsonAccess = Node.extend({ // Here Node is my above mentioned model

    getJSON: function(){
        var collection = nodeInstance.toJSON(); // nodeInstance is an instance of my collection Nodes
        return collection; //returns JSON
    }
});
jAccess = new jsonAccess();

So here is what I am doing to access the JSON

 getNodeId: function(){ //Function to get Node Id from JSON
        objectJSON = jAccess.getJSON(); // Get JSON 
        _.each(objectJSON, function(action){
            _.each(action.Nodes, function(action){

This solves my purpose but not quite the way getters would be used in backbone.

别想她 2024-12-05 06:11:15

由于缺少很多上下文,我也可能犯了一个错误,但这是我的猜测 - 您正在创建一个空的节点模型。尝试在显示中执行类似的操作:

displayName: function() {

   var myJSON = window.getJSONObject(); //wherever your json object is or how to get it...
   var node = new Node({
   id:myJSON.Id,
   name:myJSON.Name,
   isComplete: myJSON.IsComplete,
   ...
   });

   alert(node.get('name'));
   alert("Getter: "+node.getName()); //your version...
}

虽然这只是一种预感...也许我错过了您的上下文,但现在似乎是这种情况...

Since a lot of context is missing I too may be making a mistake but here's my guess - you are creating an empty Node Model. Try doing something like this in display:

displayName: function() {

   var myJSON = window.getJSONObject(); //wherever your json object is or how to get it...
   var node = new Node({
   id:myJSON.Id,
   name:myJSON.Name,
   isComplete: myJSON.IsComplete,
   ...
   });

   alert(node.get('name'));
   alert("Getter: "+node.getName()); //your version...
}

This is just a hunch though...maybe I'm missing your context but this seems to be the case for now...

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