Last active
August 29, 2015 13:57
-
-
Save izelnakri/9853082 to your computer and use it in GitHub Desktop.
My ancient/badly written backbone view example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PhilateliSt.Views.SessionsIndex extends Backbone.View | |
| template: JST['sessions/index'] | |
| el: ".lotsrow" | |
| events: | |
| 'click #create_lot': 'createLot' | |
| 'click .lotta': 'selectLot' | |
| 'keydown .js-input': 'updateLot' | |
| 'focusout .js-input': 'updateLot' | |
| initialize: (attributes) -> | |
| @options = attributes | |
| @renderLot(_.extend($('#lot_view').data('lot'), { picture_url: $('#lot_view').data('picture-url')})) #unless params[session] is valid | |
| @collection.on('reset', @render, this) | |
| @collection.on('add', @render, this) | |
| @collection.on('change', @render, this) | |
| @collection.fetch({add: false, reset: true}) #No caching | |
| render: -> | |
| @session = @collection.models[@options.current_session-1] | |
| @lots = @session.get('lots') | |
| if @options.current_lot? | |
| @renderLot(@lots[@options.current_lot-1]) | |
| $('#lotlist').html(@template(lots: @lots)) | |
| this | |
| createLot: (event) -> | |
| console.log('Clicked!') | |
| #refactor the ugly code below, add id?., session_id logic is faulty(it's session no) | |
| @session.set lots: @lots.concat({no: @assignLotNo(), session_id: @session.get('id') }), | |
| wait: true | |
| error: @handleError | |
| @collection.sync("update", @session) | |
| @renderLot(@lots[@lots.length-1]) | |
| assignLotNo: -> | |
| if @lots[@lots.length-1] == undefined #means if it has no lots | |
| return @findLastLotNo() | |
| else | |
| return @lots[@lots.length-1].no+1 #means last lot.no + 1 | |
| findLastLotNo: -> | |
| for i in [2..@options.current_session] #not sure if this is correct, seems correct | |
| if @collection.models[@options.current_session-i].get('lots').length > 0 | |
| @previous_lots = @collection.models[@options.current_session-i].get('lots') | |
| return @previous_lots[@previous_lots.length-1].no+1 | |
| selectLot: (event) -> | |
| number = parseInt($(event.currentTarget).find('.lot_no').html().match(/\d+/g)) | |
| @session.get('lots').forEach (lot) => | |
| if lot.no == number | |
| @renderLot(lot) | |
| renderLot: (lot) -> | |
| view = new PhilateliSt.Views.Session(model: lot) | |
| #file upload | |
| $("input:file").fileupload({ | |
| type: "PUT", | |
| dataType: "json", | |
| formData: | |
| lot: JSON.stringify(lot) #toJSON somehow | |
| progressall: (e, data) -> | |
| progress = parseInt(data.loaded / data.total * 100, 10) | |
| $('#progress .bar').css('width', progress + '%') | |
| add: (e, data) -> | |
| data.submit() | |
| done: (e, data) => | |
| console.log("okdir") | |
| @collection.fetch({ | |
| success: (collection, response) => | |
| console.log(@session) | |
| @session.get('lots').forEach (lot) => | |
| if lot.no == parseInt($('#lot_no').html().match(/\d+/g)) | |
| @renderLot(lot) | |
| }) | |
| }) | |
| this | |
| #REFACTOR THE SCOPE FROM SESSION TO LOT | |
| updateLot: (event) -> | |
| if event.which == 13 || event.type == "focusout" | |
| value = $(event.currentTarget).val() | |
| lot_no = parseInt($('#lot_no').html().match(/\d+/g)) | |
| @session.set lots: @setLots(@session.get('lots'), lot_no), | |
| wait: true | |
| @collection.sync("update", @session) | |
| #REFACTOR THE INDEX SEARCH | |
| setLots: (array, lot_no) -> | |
| lotsdiv = $('#lot_' + (lot_no-1)) | |
| element = array.filter((obj) -> return obj.no == lot_no)[0] | |
| element_index = array.indexOf(element) | |
| array[element_index] = { | |
| id: element.id | |
| no: element.no | |
| session_id: @session.id | |
| lot_type: lotsdiv.find('.lot_type').val() | |
| start_price: lotsdiv.find('.lot_price').val() | |
| description: $('#lot_description').val() | |
| field: $('#lot_field').val() | |
| theme: $('#lot_theme').val() | |
| quality: $('#lot_quality').val() | |
| reference: $('#lot_reference').val() | |
| } | |
| return array | |
| handleError: (entry, response) -> | |
| if response.status == 422 | |
| errors = $.parseJSON(response.responseText).errors | |
| for attribute, messages of errors | |
| alert "#{attribute} #{message}" for message in messages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment