<commit_before>/**
 * Copyright (C) 2015 Laverna project Authors.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/* global define */
define([
    'underscore',
    'marionette',
    'backbone.radio',
    'q',
    'modules/remotestorage/classes/rs',
    'modules/remotestorage/classes/module'
], function(_, Marionette, Radio, Q, RS, RsModule) {
    'use strict';

    /**
     * RemoteStorage sync controller.
     *
     * Listens to events:
     * 1. event: `save:after:encrypted`, channel: [notes|notebooks|tags]
     *    syncs changes to RemoteStorage.
     * 2. `RemoteStorage`, event: `ready`
     *    syncs local and remote changes.
     * 3. `RemoteStorage`, event: `change`
     *    syncs remote data to local storage.
     *
     * Triggers:
     * 1. channel: [notes|notebooks|tags], request: 'save:raw'
     *    passes new changes to a model.
     * 2. channel: [notes|notebooks|tags], request: 'fetch'
     *    expects to get a collection of notes,notebooks, or tags.
     * 3. channel: [notes|notebooks|tags], request: 'save:all:raw'
     *    passes new models.
     */
    var Sync = Marionette.Object.extend({

        initialize: function() {
            _.bindAll(this, 'onReady', 'syncAll', 'onRsChange');

            RS.access.claim('laverna', 'rw');
            RS.displayWidget();

            // Listen to RemoteStorage events
            RS.on('ready', this.onReady);

            // Listen to Laverna events
            this.listenTo(Radio.channel('notes'), 'sync:model destroy:model restore:model', this.onSave);
            this.listenTo(Radio.channel('notebooks'), 'sync:model destroy:model restore:model', this.onSave);
            this.listenTo(Radio.channel('tags'), 'sync:model destroy:model restore:model', this.onSave);
        },

        isConnected: function() {
            return (RS.remote.connected && RS.remote.online);
        },

        /**
         * Got changes from RemoteStorage.
         */
        onRsChange: function(change) {
            var model   = change.newValue || change.oldValue,
                profile = change.relativePath.split('/')[0];

            if (change.newValue) {
                change.newValue['@context'] = null;
            }
            Radio.request(model.type, 'save:raw', change.newValue, {profile: profile});
        },
<commit_msg>:bug: Fix bug in RemoteStorage module #255<commit_after>/**
 * Copyright (C) 2015 Laverna project Authors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
/* global define */
define([
    'underscore',
    'marionette',
    'backbone.radio',
    'q',
    'modules/remotestorage/classes/rs',
    'modules/remotestorage/classes/module'
], function(_, Marionette, Radio, Q, RS, RsModule) {
    'use strict';

    /**
     * RemoteStorage sync controller.
     *
     * Listens to events:
     * 1. event: `save:after:encrypted`, channel: [notes|notebooks|tags]
     *    syncs changes to RemoteStorage.
     * 2. `RemoteStorage`, event: `ready`
     *    syncs local and remote changes.
     * 3. `RemoteStorage`, event: `change`
     *    syncs remote data to local storage.
     *
     * Triggers:
     * 1. channel: [notes|notebooks|tags], request: 'save:raw'
     *    passes new changes to a model.
     * 2. channel: [notes|notebooks|tags], request: 'fetch'
     *    expects to get a collection of notes,notebooks, or tags.
     * 3. channel: [notes|notebooks|tags], request: 'save:all:raw'
     *    passes new models.
     */
    var Sync = Marionette.Object.extend({

        initialize: function() {
            _.bindAll(this, 'onReady', 'syncAll', 'onRsChange');

            RS.access.claim('laverna', 'rw');
            RS.displayWidget();

            // Listen to RemoteStorage events
            RS.on('ready', this.onReady);

            // Listen to Laverna events
            this.listenTo(Radio.channel('notes'), 'sync:model destroy:model restore:model', this.onSave);
            this.listenTo(Radio.channel('notebooks'), 'sync:model destroy:model restore:model', this.onSave);
            this.listenTo(Radio.channel('tags'), 'sync:model destroy:model restore:model', this.onSave);
        },

        isConnected: function() {
            return (RS.remote.connected && RS.remote.online);
        },

        /**
         * Got changes from RemoteStorage.
         */
        onRsChange: function(change) {
            var model   = change.newValue || change.oldValue,
                path    = change.relativePath.split('/');

            if (change.newValue) {
                change.newValue['@context'] = null;
            }

            Radio.request(model.type || path[1], 'save:raw', change.newValue, {profile: path[0]});
        },

{'fraction_non_alphanumeric': 0.1367538430824516, 'fraction_numerical': 0.006787781992413655, 'mean_word_length': 2.3222811671087533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6980248', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1474, 'n_words': 385}