Author Topic: Javascript help  (Read 977 times)

0 Members and 1 Guest are viewing this topic.

Offline sc13nt1st

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 0
    • View Profile
Javascript help
« on: June 12, 2013, 07:45:30 pm »
So i have been learning some javascript and currently trying out backbonejs, underscore, jquery etc

I have been trying and failing at making the form show up again after it has been saved and also a way for adding and submitting more than one set of fields.


Any help will be appreciated.

Offline sc13nt1st

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 0
    • View Profile
Re: Javascript help
« Reply #1 on: June 12, 2013, 07:50:13 pm »
I added a "noop" where i am having troubles.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Javascript help
« Reply #2 on: June 14, 2013, 09:18:56 am »
Could you please post the code where you have issues with here using
Code: (javascript) [Select]
? because most users don't want to go trough a zip file created on a Mac with shitloads of files searching for you NOOP. :P

thanks
~Factionwars

Offline sc13nt1st

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 0
    • View Profile
Re: Javascript help
« Reply #3 on: June 17, 2013, 11:26:39 am »

Code: (javascript) [Select]
(function ($) {


  var Ration = Backbone.Model.extend({
    // This is where backbone will persist a model change to the server, i.e. /rations/{id}
    //urlRoot : '/rations'
  });


  var Rations = Backbone.Collection.extend({
    // This is where backbone will fetch data, i.e. get request to rations
    url: '/rations.json',
    model:Ration,
    //localStorage: new Backbone.LocalStorage("Rations")
  });


  window.rations = new Rations();


  var FormView = Backbone.View.extend({


    events: {
      "click #ration":          "addFields",
      "click #submit":          "save",
      "submit #ration_form":    "noop"
    },


    template: ''+
    '<form id="ration_form" class="form">' +
    '<input id="ration_name" type="text" placeholder="Name" />' +
    '<a id="ration" class="btn btn-primary" href="#">+ Ration</a>' +
    '<a style="display:none" id="submit" class="btn">Save</a>' +
    '</form>',


    initialize: function() {
      _.bindAll(this, 'render');
      console.log('init form');
      this.index = 1;
      this.collection = window.rations;
      this.listenTo(window.rations, "add", this.render);
      this.render();
    },


    noop : function(e){
      e.preventDefault();
      return false;
    },


    render: function() {
      this.$el.html(this.template);
      return this;
    },


    addFields:function(){
      console.log('here');
      $('#ration').after('<div><input placeholder="item" class="item" name="item' + this.index + '" type="text" />' +
        '<input placeholder="Quantity" class="qty" name="qty' + this.index +'" type="text" /><>');
      $('#submit').show();
     
      this.index = 1;
      return false;
    },


    save:function(){
      var ration = {
        name : $("#ration_name").val(),
        inventory : []
      };


      var items = [];
      $('.item').each(function(i, el){
        items.push($(el).val());
      });


      var qtys = [];
      $('.qty').each(function(i, el){
        qtys.push($(el).val());
      }); 


      $.each(items, function(i, el){
        ration.inventory.push({
          item:items[i],
          qty:qtys[i]
        });
      });   


      console.log(ration);


      // Create triggers presistance to the server based on urlRoot in the model
      this.collection.create(new Ration(ration));
       
      return false;
    }


  });


  var RationView = Backbone.View.extend({


    initialize: function() {
      _.bindAll(this);
      console.log('init rations');
      this.collection = rations;
      this.listenTo(window.rations, "add", this.render);
      this.template = _.template($("#ration-template").html());
    },
     
    render: function() {
      console.log('render');
      var self = this;
      var html = this.template({rations:self.collection.toArray()});
      this.$el.html(html);
      return this;
    }
     
  });


  $(document).ready(function(){
    console.log('ready');


    // passing DOM elements into the views allows for reuse
    new FormView({el:$('#form-wrapper')});
    new RationView({el:$('#rations')})
  });


}(jQuery));
« Last Edit: June 17, 2013, 11:28:59 am by Factionwars »