preload javascript vars using gon
This commit is contained in:
parent
9f930a9ba8
commit
6fcba2dd4b
7 changed files with 51 additions and 17 deletions
1
Gemfile
1
Gemfile
|
|
@ -104,6 +104,7 @@ gem 'mobile-fu'
|
||||||
|
|
||||||
gem 'will_paginate'
|
gem 'will_paginate'
|
||||||
gem 'client_side_validations'
|
gem 'client_side_validations'
|
||||||
|
gem 'gon'
|
||||||
|
|
||||||
# assets
|
# assets
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -196,6 +196,9 @@ GEM
|
||||||
gem_plugin (0.2.3)
|
gem_plugin (0.2.3)
|
||||||
gherkin (2.9.3)
|
gherkin (2.9.3)
|
||||||
json (>= 1.4.6)
|
json (>= 1.4.6)
|
||||||
|
gon (3.0.2)
|
||||||
|
actionpack (>= 2.3.0)
|
||||||
|
json
|
||||||
guard (1.0.1)
|
guard (1.0.1)
|
||||||
ffi (>= 0.5.0)
|
ffi (>= 0.5.0)
|
||||||
thor (~> 0.14.6)
|
thor (~> 0.14.6)
|
||||||
|
|
@ -522,6 +525,7 @@ DEPENDENCIES
|
||||||
foreigner (~> 1.1.0)
|
foreigner (~> 1.1.0)
|
||||||
foreman (= 0.41)
|
foreman (= 0.41)
|
||||||
fuubar (>= 1.0)
|
fuubar (>= 1.0)
|
||||||
|
gon
|
||||||
guard-cucumber
|
guard-cucumber
|
||||||
guard-rspec
|
guard-rspec
|
||||||
guard-spork
|
guard-spork
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,19 @@ var app = {
|
||||||
$(".stream_title").text(link.text())
|
$(".stream_title").text(link.text())
|
||||||
app.router.navigate(link.attr("href").substring(1) ,true)
|
app.router.navigate(link.attr("href").substring(1) ,true)
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
hasPreload : function(prop) {
|
||||||
|
return !!(window.preloads && window.preloads[prop]) //returning boolean variable so that parsePreloads, which cleans up properly is used instead
|
||||||
|
},
|
||||||
|
|
||||||
|
parsePreload : function(prop){
|
||||||
|
if(!app.hasPreload(prop)) { return }
|
||||||
|
|
||||||
|
var preload = window.preloads[prop]
|
||||||
|
delete window.preloads[prop] //prevent dirty state across navigates
|
||||||
|
|
||||||
|
return JSON.parse(preload)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,20 +61,17 @@ app.models.Stream = Backbone.Collection.extend({
|
||||||
this.items.add(models)
|
this.items.add(models)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
preloadOrFetch : function(){ //hai, plz test me THNX
|
preloadOrFetch : function(){ //hai, plz test me THNX
|
||||||
this.preload()
|
if(app.hasPreload("stream")){
|
||||||
if(this.items.length == 0) {
|
this.preload()
|
||||||
|
} else {
|
||||||
this.fetch()
|
this.fetch()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
preload : function(){
|
preload : function(){
|
||||||
var preloadJson = window.preLoadContent && JSON.parse(window.preLoadContent)
|
this.items.reset(app.parsePreload("stream"))
|
||||||
delete window.preLoadContent // always do this just to be safe in preventing dirty state across navigates
|
this.trigger("fetched")
|
||||||
|
|
||||||
if(preloadJson) {
|
|
||||||
this.items.reset(preloadJson)
|
|
||||||
this.trigger("fetched")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -81,13 +81,11 @@ class PeopleController < ApplicationController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@person = Person.find_from_guid_or_username(params)
|
@person = Person.find_from_guid_or_username(params)
|
||||||
flag = FeatureFlagger.new(current_user, @person)
|
|
||||||
logger.info(request.format)
|
|
||||||
|
|
||||||
raise(ActiveRecord::RecordNotFound) if remote_profile_with_no_user_session?
|
raise(ActiveRecord::RecordNotFound) if remote_profile_with_no_user_session?
|
||||||
return redirect_to :back, :notice => t("people.show.closed_account") if @person.closed_account?
|
return redirect_to :back, :notice => t("people.show.closed_account") if @person.closed_account?
|
||||||
return redirect_to person_path(@person) if params[:ex] && !flag.new_profile?
|
return redirect_to person_path(@person) if cant_experimental
|
||||||
return redirect_to person_path(@person, :ex => true) if !params[:ex] && flag.new_profile? && flag.new_hotness? && request.format == "text/html"
|
return redirect_to person_path(@person, :ex => true) if needs_experimental
|
||||||
|
|
||||||
@post_type = :all
|
@post_type = :all
|
||||||
@aspect = :profile
|
@aspect = :profile
|
||||||
|
|
@ -121,7 +119,9 @@ class PeopleController < ApplicationController
|
||||||
format.all do
|
format.all do
|
||||||
if params[:ex]
|
if params[:ex]
|
||||||
@page = :experimental
|
@page = :experimental
|
||||||
render :text => @stream.stream_posts.as_api_response(:backbone).to_json, :layout => 'post'
|
json = @stream.stream_posts.as_api_response(:backbone).to_json
|
||||||
|
gon.stream = json
|
||||||
|
render :nothing => true, :layout => 'post'
|
||||||
else
|
else
|
||||||
respond_with @person, :locals => {:post_type => :all}
|
respond_with @person, :locals => {:post_type => :all}
|
||||||
end
|
end
|
||||||
|
|
@ -189,9 +189,21 @@ class PeopleController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
protected
|
||||||
|
|
||||||
|
def flag
|
||||||
|
@flag ||= FeatureFlagger.new(current_user, @person)
|
||||||
|
end
|
||||||
|
|
||||||
|
def cant_experimental
|
||||||
|
params[:ex] && !flag.new_profile?
|
||||||
|
end
|
||||||
|
|
||||||
|
def needs_experimental
|
||||||
|
!params[:ex] && flag.new_profile? && flag.new_hotness? && request.format == "text/html"
|
||||||
|
end
|
||||||
|
|
||||||
def remote_profile_with_no_user_session?
|
def remote_profile_with_no_user_session?
|
||||||
@person && @person.remote? && !user_signed_in?
|
@person.try(:remote?) && !user_signed_in?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -56,9 +56,11 @@
|
||||||
= yield(:head)
|
= yield(:head)
|
||||||
= csrf_meta_tag
|
= csrf_meta_tag
|
||||||
|
|
||||||
|
= include_gon(:camel_case => true, :namespace => :preloads)
|
||||||
|
|
||||||
%body
|
%body
|
||||||
= flash_messages
|
= flash_messages
|
||||||
#container
|
#container
|
||||||
= javascript_tag "window.preLoadContent = '#{escape_javascript(yield)}'"
|
= yield
|
||||||
|
|
||||||
= include_chartbeat
|
= include_chartbeat
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,13 @@ describe("app.pages.Profile", function(){
|
||||||
|
|
||||||
it("preloads the stream for the user", function(){
|
it("preloads the stream for the user", function(){
|
||||||
spyOn(this.stream, "preload")
|
spyOn(this.stream, "preload")
|
||||||
|
|
||||||
|
window.preloads = {stream : JSON.stringify(["unicorns"]) }
|
||||||
|
|
||||||
new app.pages.Profile({stream : this.stream})
|
new app.pages.Profile({stream : this.stream})
|
||||||
expect(this.stream.preload).toHaveBeenCalled()
|
expect(this.stream.preload).toHaveBeenCalled()
|
||||||
|
|
||||||
|
delete window.preloads //cleanup
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("rendering", function(){
|
describe("rendering", function(){
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue