Clean up view.js

This commit is contained in:
Steffen van Bergerem 2015-10-11 20:20:24 +02:00
parent 2aed793d19
commit 4a55fc5fb6
9 changed files with 55 additions and 42 deletions

View file

@ -51,6 +51,7 @@ var app = {
this.setupBackboneLinks();
this.setupGlobalViews();
this.setupDisabledLinks();
this.setupForms();
},
hasPreload : function(prop) {
@ -136,6 +137,17 @@ var app = {
$("a.disabled").click(function(event) {
event.preventDefault();
});
},
setupForms: function() {
// add placeholder support for old browsers
$("input, textarea").placeholder();
// setup remote forms
$(document).on("ajax:success", "form[data-remote]", function() {
$(this).clearForm();
$(this).focusout();
});
}
};

View file

@ -50,6 +50,9 @@ app.views.Base = Backbone.View.extend({
// add avatar fallback if it can't be loaded
this.$el.find(this.avatars.selector).error(this.avatars.fallback);
// add placeholder support for old browsers
this.$("input, textarea").placeholder();
this.postRenderTemplate();
},

View file

@ -26,7 +26,6 @@ app.views.CommentStream = app.views.Base.extend({
},
postRenderTemplate : function() {
this.$("textarea").placeholder();
this.model.comments.each(this.appendComment, this);
// add autoexpanders to new comment textarea

View file

@ -39,7 +39,6 @@
//= require_tree ./helpers
//= require_tree ./pages
//= require_tree ./widgets
//= require view
//= require mentions
//= require bootstrap
//= require osmlocator

View file

@ -1,22 +0,0 @@
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
var View = {
initialize: function() {
/* label placeholders */
$("input, textarea").placeholder();
$(document).on('ajax:success', 'form[data-remote]', function () {
$(this).clearForm();
$(this).focusout();
});
/* tag following */
$("#new_tag_following .tag_input").bind('focus', function(){
$(this).siblings("#tag_following_submit").removeClass('hidden');
});
},
};
$(function() {
View.initialize();
});
// @license-end

View file

@ -1,4 +1,28 @@
describe("app", function() {
describe("initialize", function() {
it("calls several setup functions", function() {
spyOn(app.Router.prototype, "initialize");
spyOn(app, "setupDummyPreloads");
spyOn(app, "setupUser");
spyOn(app, "setupHeader");
spyOn(app, "setupBackboneLinks");
spyOn(app, "setupGlobalViews");
spyOn(app, "setupDisabledLinks");
spyOn(app, "setupForms");
app.initialize();
expect(app.Router.prototype.initialize).toHaveBeenCalled();
expect(app.setupDummyPreloads).toHaveBeenCalled();
expect(app.setupUser).toHaveBeenCalled();
expect(app.setupHeader).toHaveBeenCalled();
expect(app.setupBackboneLinks).toHaveBeenCalled();
expect(app.setupGlobalViews).toHaveBeenCalled();
expect(app.setupDisabledLinks).toHaveBeenCalled();
expect(app.setupForms).toHaveBeenCalled();
});
});
describe("user", function() {
it("returns false if the current_user isn't set", function() {
app._user = undefined;
@ -11,4 +35,13 @@ describe("app", function() {
expect(app.user().get("name")).toEqual("alice");
});
});
describe("setupForms", function() {
it("calls jQuery.placeholder() for inputs", function() {
spyOn($.fn, "placeholder");
app.setupForms();
expect($.fn.placeholder).toHaveBeenCalled();
expect($.fn.placeholder.calls.mostRecent().object.selector).toBe("input, textarea");
});
});
});

View file

@ -14,13 +14,6 @@ describe("app.views.CommentStream", function(){
});
describe("postRenderTemplate", function(){
it("applies infield labels", function(){
spyOn($.fn, "placeholder");
this.view.postRenderTemplate();
expect($.fn.placeholder).toHaveBeenCalled();
expect($.fn.placeholder.calls.mostRecent().object.selector).toBe("textarea");
});
it("autoResizes the new comment textarea", function(){
spyOn(window, "autosize");
this.view.postRenderTemplate();

View file

@ -69,7 +69,6 @@ describe("app.views.Base", function(){
expect($.fn.timeago.calls.mostRecent().object.selector).toBe("time");
});
it("initializes tooltips declared with the view's tooltipSelector property", function(){
this.view.tooltipSelector = ".christopher_columbus, .barrack_obama, .block_user";
@ -77,6 +76,13 @@ describe("app.views.Base", function(){
this.view.render();
expect($.fn.tooltip.calls.mostRecent().object.selector).toBe(".christopher_columbus, .barrack_obama, .block_user");
});
it("applies infield labels", function(){
spyOn($.fn, "placeholder");
this.view.render();
expect($.fn.placeholder).toHaveBeenCalled();
expect($.fn.placeholder.calls.mostRecent().object.selector).toBe("input, textarea");
});
});
});
});

View file

@ -1,10 +0,0 @@
/* Copyright (c) 2010-2011, Diaspora Inc. This file is
* licensed under the Affero General Public License version 3 or later. See
* the COPYRIGHT file.
*/
describe("View", function() {
it("is the object that helps the UI", function() {
expect(typeof View === "object").toBeTruthy();
});
});