2009-11-30 75 views
0

如下問題:對象引用的問題

我想建立與jQuery一點剪貼板。我嘗試了幾次,用$(object).data('id',objectToStore)將對象存儲在javascript對象的數據中。對象可以存儲在那裏,工作正常。問題是如果我嘗試插入存儲的數據,我只能得到該對象的引用。所以當我編輯一個副本時,其他人也會改變。我需要一種方法將html代碼複製到一個全局變量中,然後從存儲的代碼中單獨插入代碼。希望你們明白我的問題!謝謝!

下面的代碼:

對象

/** 
    * Objectdefinition 
    */ 
Clipboard = { 

    //PROPERTIES 
     itemcount: 0,   
     maxItems:10,    

     //Templates 
     tplClipboard:"<div id='GUI_clipboard'><a href='' title='clear&nbsp;clipboard' id='GUI_clipboardClose' class='gritter-close'></a><ul></ul></div>", 
     tplItem:"<li><a href='' class='[[type]] clipboardItem' id='[[id]]'><span class='hidden'>[[text]]</span></a></li>", 
     tplItemHover:"<div class='GUI_clipboard_itemhover' style='width:[[offsetW]]px;height:[[offsetH]]px'><a href='' title='delete&nbsp;container' class='GUI_containerDelete'><span class='hidden'>Delete</span></a></div>", 

     //Clipboarditem 
     item:{ 
      id:null, 
      type:null, 
      content:'', 
      offsetWidth:0, 
      offsetHeight:0 
     }, 

    //FUNCTIONS 
     addItem:function(id,type,text,content,offsetH,offsetW){ 
      if(this.itemcount>=this.maxItems){ 
       return $.gritter.add({ 
        title:'Clipboard', 
        text:'You cannot store more than '+ this.maxItems +' Elements!', 
        image:'warning', 
       }); 
      } 


      var item = {}; 
       item.id=id, 
       item.type=type, 
       item.content=content, 
       item.offsetHeight=offsetH, 
       item.offsetWidth= offsetW; 


      this.verify(); 


      if (!this.checkRed(id)) { 


       this.itemcount++; 


       var tmp = this.tplItem; 


       tmp = this.str_replace(['[[type]]', '[[id]]', '[[text]]'], [type, id, text], tmp); 

       $('#GUI_clipboard ul').append(tmp); 
       var $item = $('a#'+id); 
       var number = this.itemcount; 
       $item.hide().fadeIn('slow',function(){ 

        Clipboard.redraw(); 
       }); 


       this.saveItem(item); 


       var config = { 
        over:function(){Clipboard.hoveringItem($('a',this))}, 
        out:function(){Clipboard.unhoveringItem($('a',this))}, 
        timeout:1 
       }; 
       $item.parent().hoverIntent(config); 


       $item.draggable({ 
        connectToSortable:'.column',       
        helper:'clone',          
        revert:'invalid',         
        cursor:'move',          //Cursor 
        start:function(){ 
         $('body').unbind('mouseover',Content.showContainerMenu); 
         $('body').unbind('mouseout',Content.hideContainerMenu); 
         $('#GUI_clipboard li').trigger('mouseout'); 
        }, 
        stop:function(){ 
         $('body').bind('mouseover',Content.showContainerMenu); 
         $('body').bind('mouseout',Content.hideContainerMenu); 
        } 
       }); 
      }else{ 

       $('#'+id,'#GUI_clipboard').effect("bounce", { times:3 }, 300); 
      } 
     }, 


     saveItem:function(item){    
      $(this).data(item.id,item); 
     }, 


     removeItem: function(id){ 
      $('#GUI_clipbaord').data(id,null); 
      $('a[id='+id+']','#GUI_clipboard').parent().slideUp('slow',function(){$(this).remove()}); 
      this.itemcount--; 

      if(this.itemcount==0)this.remove(); 
     }, 


     verify:function(){ 
      if($('#GUI_clipboard').length == 0){ 
       $('body').append(this.tplClipboard);      

       $('#GUI_clipboard')       
        .css({ 
         top:$(window).height()/2+'px'      
        }) 
        .animate({           
         left:0 
        }).children('.gritter-close').capture({cssClass:'GUI_clipboardClose'});  
      } 
     }, 


     checkRed:function(id){ 
      if($('a[id='+id+']').length==0)return false; 
      else return true; 
     }, 


     remove:function(){ 
      $('#GUI_clipboard').animate({            
       left:'-60px' 
      },function(){ 
       $(this).remove(); 
      }); 
      this.itemcount=0; 
     }, 


     hoveringItem:function(el){ 

      var item = $(this).data($(el).attr('id')), 
       content=item.content, 
       oH=item.offsetHeight, 
       oW=item.offsetWidth, 


      tmp = this.tplItemHover;     
      tmp = this.str_replace(['[[offsetW]]', '[[offsetH]]'], [oW,oH], tmp); 


      $(el).after(tmp); 
      var $element = $('.GUI_clipboard_itemhover').append(content).prepend("<div class='GUI_clipboardArrow'></div>"); 


      $element.position({ my: 'left center', at: 'right center', of: $(el),offset:'14 0',collision:'none fit'}); 
      $('.GUI_clipboardArrow',$element).position({ my: 'left center', at: 'right center', of: $(el),offset:'-2 0',collision:'none fit'}); 


      $('#'+item.id,'#GUI_clipboard').removeClass('borderContainer editable'); 


      $('a.GUI_containerDelete',$element).click(function(){ 
       Clipboard.removeItem($element.children('.container').attr('id')); 
       $element.fadeOut().remove(); 
      }).capture({cssClass:'GUI_clipboardItemClose'}); 

     }, 


     unhoveringItem:function(el){ 
      //Preview entfernen 
      $(el).next().remove(); 
     }, 


     redraw:function(){ 
      if(this.itemcount>1){ 
       $('#GUI_clipboard').animate({ 
        top: '-=20px'      
       }); 
      } 
     }, 


     str_replace: function(search, replace, subject, count) { 

      var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0, 
       f = [].concat(search), 
       r = [].concat(replace), 
       s = subject, 
       ra = r instanceof Array, sa = s instanceof Array; 
      s = [].concat(s); 
      if (count) { 
       this.window[count] = 0; 
       } 

      for (i=0, sl=s.length; i < sl; i++) { 
       if (s[i] === '') { 
        continue; 
       } 
       for (j=0, fl=f.length; j < fl; j++) { 
        temp = s[i]+''; 
        repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0]; 
        s[i] = (temp).split(f[j]).join(repl); 
        if (count && s[i] !== temp) { 
         this.window[count] += (temp.length-s[i].length)/f[j].length;} 
       } 
      } 
      return sa ? s : s[0]; 

     } 
} 

在該對象!正如你看到的,當我將鼠標懸停在一個元素上時,Object從內部存儲中獲取它。但是,當我與下面進入內容領域插入對象

var item = $(Clipboard).data($(ui.sender).attr('id')), 
         newItem = $.extend(true, {}, item); 
         content=newItem.content; 

,然後懸停剪貼板拖動它,並重新插入它的含量 - 面積對象(HTML代碼)消失,被插入的預覽剪貼板。

任何想法!

請!

回答

1

得到了我自己的解決方案!感謝來自Leigh Kaszick的傑出JavaScript教程:「面向對象JavaScript的基礎知識」。檢查出來:http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/

字面對象總是被引用,而功能不是;)這就是訣竅。

文本對象:

var Storage = { 
    id:'one', 
    content:'blah' 
}; 

功能對象:

function Storage(id,content) = { 
     this.id=id; 
     this.content:content; 
} 
var myStorage = new Storage('one','blah'); 

爲了解決我的問題,我每次當我通過數據複製到剪貼板時創建的存儲對象的新實例。它的工作原理! ;)

+0

它的非函數對象,它的類。類創建新的對象與「新」 – Yosef 2011-11-21 10:18:29