2012-06-08 5 views
1

내가 작업중인 백본 컬렉션을 가져 오는 데 이상한 문제가 있습니다. 코드의 한 특정 인스턴스에서 가져 오기를 수행합니다 (코드의 다른 영역에서 정확히 어떻게 작동합니까?). 가져 오기는 결코 서버로 전달되지 않으며 개발자 도구는 요청을 빨간색으로 표시합니다. 상태/텍스트 필드에서 단어 (취소됨).정보가없는 백본 컬렉션 가져 오기 오류

나는 이것을 백본 동기화 방법으로 걸어 갔으며 $ .ajax가 빌드되고 모든 것이 잘 보입니다. 이 문제를 겪은 사람이 있습니까?

여기가 도움이된다면이 파일은 .ashx 서비스를 호출하여 파일의 존재 여부를 먼저 확인한 후 열어야하는 기능입니다. 나를 위해 작동하지 않는 부분은 "me.collection.fetch입니다()

openDocument: function() { 
     var me = this, 
       fileId = me.model.get('id'), 
      userId = Dashboard.Data.Models.UserModel.get("UserInfo").User_ID, 
      fileRequest = '/genericHandlers/DownloadFile.ashx?id=' + fileId + '&userId=' + userId, 
      fileCheck = '/genericHandlers/CheckFileExistance.ashx?id=' + fileId + '&userId=' + userId; 

     //hide tooltip 
     me.hideButtonTooltips(); 

     // Check for file existance 
     $.ajax({ 
      url: fileCheck 
     }) 
     .done(function (data) { 
      if (data && data === "true") { 

       document.location.href = fileRequest; 

       me.collection.fetch(); 



      } else if (!!data && data === "false") { 
       "This file is no longer available.".notify('error'); 
      } 
     }) 
     .fail(function (data) { 
      "Something went wrong during the File Existance check".notify('error'); 
      "Something went wrong during the File Existance check".log(userId, 'error', 'Docs'); 
     }); 
    }, 

내 컬렉션 :.

// docsCollection.js - The collection of ALL the documents available to a given user 

// Document Collection 
Dashboard.Collections.DocsCollection = Backbone.Collection.extend({ 

    model: Dashboard.Models.DocumentUploadModel, 

    url: function() { 
     return 'apps/docs/Docs/' + this.userId; 
    }, 

    initialize: function (options) { 
     this.userId = options.userId; 
     this.deferredFetch = this.fetch(); 

    }, 

    comparator: function (model) { 
     return -(new Date(model.get('expirationDate'))); 
    }, 

    getDaysSinceViewedDocuments: function() { 
     return this.filter(function (model) { 
      return model.get('daysSinceViewed') !== null; 
     }); 
    }, 

    getNewDocuments: function() { 
     return this.filter(function (model) { 
      return model.get('isNew'); 
     }); 
    }, 

    getExpiredDocuments: function() { 
     return this.filter(function (model) { 
      return model.get('isExpired'); 
     }); 
    } 

}); 

내 모델 :

Dashboard.Models.DocumentUploadModel = Backbone.Model.extend({ 
    defaults: { 
     fileArray: [], 
     name: '', 
     description: '', 
     accesses: [], 
     tags: [], 
     expirationDate: '' 
    }, 

    initialize: function() { 

      this.set({ 
       userId: Dashboard.Data.Models.UserModel.get("UserInfo").User_ID, 
       expirationDate: (this.isNew()) ? buildExpirationDate() : this.get('expirationDate') 
      }, { silent: true }); 

     function buildExpirationDate() { 
      var date = new Date((new Date()).getTime() + 24 * 60 * 60 * 1000 * 7), 
        dateString = "{0}/{1}/{2}".format(date.getMonth() + 1, date.getDate(), date.getFullYear()); 

      return dateString; 
     } 

    }, 

    firstFile: function() { 
     return this.get('fileArray')[0]; 
    }, 

    validate: function (attributes) { 
     var errors = []; 

     if (attributes.name === '' || attributes.name.length === 0) 
      errors.push({ 
       input: 'input.txtName', 
       message: "You must enter a name." 
      }); 

     if (attributes.description === '' || attributes.description.length === 0) 
      errors.push({ 
       input: 'textarea.taDescription', 
       message: "You must enter a description." 
      }); 

     if (errors.length > 0) 
      return errors; 

     return; 
    }, 

    sync: function (method, model, options) { 
     var formData = new FormData(), 
       files = model.get("fileArray"), 
       $progress = $('progress'), 
       success = options.success, 
       error = options.error; 

     // Nothing other than create or update right now 
     if (method !== "create" && method !== "update") 
      return; 

     // Build formData object 
     formData.append("name", model.get("name")); 
     formData.append("description", model.get("description")); 
     formData.append("accesses", model.get("accesses")); 
     formData.append("tags", model.get("tags")); 
     formData.append("expirationDate", model.get("expirationDate")); 
     formData.append("userId", model.get("userId")); 
     formData.append("isNew", model.isNew()); 

     // if not new then capture id 
     if (!model.isNew()) 
      formData.append('id', model.id); 


     for (var i = 0; i < files.length; i++) { 
      formData.append('file', files[i]); 
     } 

     xhr = new XMLHttpRequest(); 

     xhr.open('POST', '/genericHandlers/UploadDocsFile.ashx'); 


     xhr.onload = function() { 
      if (xhr.status === 200) { 
       if (success) 
        success(); 
      } else { 
       if (error) 
        error(); 
      } 
     } 

     if ($progress.length > 0) { 
      xhr.upload.onprogress = function (evt) { 
       var complete; 

       if (evt.lengthComputable) { 
        // Do the division but if you cant put 0 
        complete = (evt.loaded/evt.total * 100 | 0); 
        $progress[0].value = $progress[0].innerHTML = complete; 
       } 
      } 
     } 



     xhr.send(formData); 
    }, 

    upload: function (changedAttrs, options) { 
     this.save("create", changedAttrs, options); 
    } 

}); 

답변

1

당신 ' 컬렉션을 가져 오기 전에 document.location.href에 값을 할당하십시오.

document.location.href = fileRequest; 
me.collection.fetch(); 

document.location.href을 변경하면 전체 페이지가 변경되고 현재 실행중인 모든 JavaScript가 종료되므로 me.collection.fetch()이 실행되지 않을 것으로 예상됩니다.

+0

설명해 드리겠습니다! 감사. –