From user-return-15919-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Thu Apr 21 00:15:26 2011 Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8C0601422 for ; Thu, 21 Apr 2011 00:15:26 +0000 (UTC) Received: (qmail 41388 invoked by uid 500); 21 Apr 2011 00:15:25 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 41349 invoked by uid 500); 21 Apr 2011 00:15:25 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 41341 invoked by uid 99); 21 Apr 2011 00:15:25 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Apr 2011 00:15:25 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,RFC_ABUSE_POST,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of mrtrick@gmail.com designates 209.85.210.52 as permitted sender) Received: from [209.85.210.52] (HELO mail-pz0-f52.google.com) (209.85.210.52) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Apr 2011 00:15:17 +0000 Received: by pzk12 with SMTP id 12so922489pzk.11 for ; Wed, 20 Apr 2011 17:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=SNayITo/UCZku2Hvk3TQ3NqfUrYwgYMQKFVNpOpzCxM=; b=i+ZHwjt1kMcFZskNqVHjbDI8kBA9jabg4QpKjq8uvbrA4Cwi0iKf8jYGJFRblpXd5X nd64/rVrxjg+SGVFUx4+A8EM8bvWsAK/kFDbkH7gI0DU1O1aJBhGxze8EOA9AaTg2Szl HuMkRFOvG/4rwTCEqCT54OlzwNNtz8HX4fty8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=a7pH9XfgT+c0cMJ9c/emxkJf8lLlHzhozCAuYuhPL7/WR0oX0NKnFpqCoSi+Xy9BSV ubI7ZNZ0KwdBv8GbQXwHlXD5/w0hRO3j1fzpsCSY5syiwGBCSnUvgG1U6/kdaLZFM+lg 3dkJ7Zu5yK0w5mW9aBId1/2EO3ytOSryqliuo= Received: by 10.143.154.35 with SMTP id g35mr4347618wfo.120.1303344897042; Wed, 20 Apr 2011 17:14:57 -0700 (PDT) Received: from [192.168.1.100] ([115.187.236.96]) by mx.google.com with ESMTPS id s39sm1818179wfc.16.2011.04.20.17.14.53 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 20 Apr 2011 17:14:55 -0700 (PDT) Message-ID: <4DAF76EF.7020400@gmail.com> Date: Thu, 21 Apr 2011 10:14:39 +1000 From: Patrick Barnes User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: user@couchdb.apache.org Subject: Re: Dealing with sealed docs References: <6084E307-7CFB-4BAD-A12C-063272628B95@vpro.nl> <95333123-2B25-4E26-8C8A-D502C88D96B3@apache.org> <4DAEF166.3020706@gmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit My apologies - I have mis-remembered. No, there is no clone operator, but some functions will return a copy rather than a reference. I had a view that looked like: function(doc) { if (doc.status=='active' && doc.doc_type=='group') { doc.path.pop(); emit(doc.path, doc.display_name); } } And it (on pre-1.0.2 couchdb) failed to generate the views properly, because doc.path.pop() was modifying the doc and the next views were receiving that modified version. In my case, changing the view to this fixed it: function(doc) { if (doc.status=='active' && doc.doc_type=='group') { path = doc.path.slice(0); path.pop(); emit(path, doc.display_name); } } So doc.path.slice(0) copies the whole doc.path array for me so that I can modify it before emitting. No such neat method exists for objects unfortunately, but how about: function(doc) { if(doc.type === 'schedule') { var events = doc.events; if (events) { events.forEach(function(event) { var broadcasters = event.broadcasters; if (broadcasters) { broadcasters.forEach(function(broadcaster) { if(broadcaster === 'VPRO') { - event.channel = doc.channel; - event.channelName = doc.channelName; - emit(event.end, event); + var _event = {channel:doc.channel, channelName:doc.channelName}; + for (var k in event) _event[k]=event[k]; + emit(event.end, _event); } }); } }); } } } The members of _event are only references, but that should be okay because you're not modifying those, only adding two extra attributes to the new object created. (Not having to _copy_ every element should also make this approach much faster than using a generic 'clone' method) Hope that works. :-) -Patrick On 21/04/2011 1:00 AM, Hay (Husky) wrote: > On Wed, Apr 20, 2011 at 4:44 PM, Patrick Barnes wrote: >> If you change >> var events = doc.events; >> to >> var events = clone doc.event; > > Would be great, but afaik there doesn't exist a 'clone' operator in > Javascript. Virtually all JS frameworks provide a method (such as > jQuery's 'extend' or Prototype's Object.clone), but in vanilla > Javascript no such method exists. > > -- Hay >