locked
Can someone help me in backbone.js RRS feed

  • Question

  • User-873890331 posted

    Hi All,

    How to get model from server and display on view ?

    I have already get the collection from server and displayed on view. Now I want to get details of single item when i pass Id to server. First I am getting collection and display in a grid. Now If someone clicks on gridview row then I passes id in url. I want to get Model by passing this Id. What I do to get Model. I am getting Error -  Uncaught TypeError: Cannot read property 'get' of undefined

    ************ Model start ************************

    define([
      'jquery',
      'underscore',
      'backbone'
    ], function ($, _, Backbone) {
        var Order = Backbone.Model.extend({
            idAttribute: "ID",
        });
        return Order;
    });
    

    ************* Model End **************************

    *************** View Start ***********************

    define([
      'jquery',
      'underscore',
      'backbone',
      'text!templates/orderDetails.html',
      'collections/OrderCollection',
      'collections/ItemCollection',
      'models/Orders',
    ], function ($, _, Backbone, orderDetails, OrderCollection, ItemCollection,Orders) {
    
        var ProfileView = Backbone.View.extend({
            el: $("#content"),
            render: function (Id) {
                this.Id = Id;
                var self = this;
                this.order = new Orders();
                this.order.fetch({
                    url: 'GetOrderDetails',
                    data: JSON.stringify({ orderId: window.Id }),
                    success: function () {
                        self.renderList();
                    },
                    error: function (a, b, c) {
                        alert(a);
                    }
                });
                
            },
            renderList: function () {
    
                var compiledTemplate = _.template(orderDetails, { Details: this.order, Items: this.order.Items });
                this.$el.html(compiledTemplate);
    
            }
        });
    
        return ProfileView;
    });
    

    *************** View End ************************

    Can someone help me regarding this issue.

    Thanks & Regards

    shwetamber

    Wednesday, June 4, 2014 3:10 PM

Answers

  • User1402382751 posted

    I have already get the collection from server and displayed on view

    Dude, you got list of models(collection) from the server, so use the Collection.get(id) method to get the model details from the collection and display in the view for the model

    http://backbonejs.org/#Collection-get

    If your collections have a view, then your context for the click event is the collection itself, so you can use that method of collection object. use

    console.log()

    to your help to find which objects are passed on to your click event callback

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 8, 2014 1:31 AM