Example #1
0
    function _updateGist(existingGist) {
        var defer = Promise.defer();

        var payload = {
            "description": DEFAULT_GIST_DESCRIPTION,
            "public": true,
            "files": {
                // "file1.txt": {
                //    "content": "String file contents"
                // }
            }
        };

        var url = GITHUB_API_SERVER + "/gists",
            type = "POST";

        if (existingGist) {
            url = existingGist.url;
            type = "PATCH";
            // mark all files to be deleted
            Object.keys(existingGist.files).forEach(function (file) {
                payload.files[file] = null;
            });
        }

        var snippets = Snippets.getAll();
        snippets.forEach(function (snippet) {
            payload.files[snippet.name] = {
                content: snippet.template
            };
        });

        $.ajax({
            url: url,
            type: type,
            dataType: "json",
            cache: false,
            data: JSON.stringify(payload),
            headers: {
                "Authorization": "token " + githubToken
            }
        })
        .done(function (data) {
            /*jshint -W106*/
            NativeApp.openURLInDefaultBrowser(data.html_url);
            /*jshint +W106*/
            defer.resolve(data);
        })
        .fail(function (err, errdesc, statusText) {
            ErrorHandler.show(err.responseText, statusText);
            defer.reject(err);
        });

        return defer.promise;
    }
    SnippetWidget.prototype.refreshSnippets = function (force) {
        var query = this.$searchInput.val();
        if (force || query !== this.lastQuery || typeof this.lastQuery !== "string") {
            this.lastQuery = query;

            if (this.detachedMode) {
                var lookingFor = this.$searchInput.val();
                this.snippets = [_.find(Snippets.getAll(), function (snippet) {
                    return snippet.name === lookingFor;
                })];
            } else {
                this.snippets = Snippets.search(query);
            }

            this.$snippetsList.html(Mustache.render(snippetWidgetListTemplate, {
                snippets: this.snippets,
                Strings: Strings
            })).toggleClass("is-empty", this.snippets.length === 0);

            this.selectSnippet();
        }
    };