using angular for typeahead. working with fixed array but not with asyncSelected

This commit is contained in:
Dani Macho
2014-10-07 03:21:53 +02:00
parent d468cb5a42
commit 6e09519ef0
13 changed files with 89 additions and 1797 deletions

View File

@ -19,9 +19,6 @@
#= require ui-bootstrap-tpls-0.11.0
#= require_tree ./modules
#= require_tree ./app
#= require typeahead.bundle-0.10.5
#= require transfer
angular.module "timeoverflow", ["ng-rails-csrf", 'ui.bootstrap']

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@ -1,11 +0,0 @@
jQuery ->
$('#offer_tag_list').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'tags',
displayKey: 'value',
source: ['foo','food','four']
});

View File

@ -0,0 +1,3 @@
// Place all the styles related to the Tags controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -0,0 +1,10 @@
class TagsController < ApplicationController
respond_to :json, :html
def index
@all_tags = (Post::all_tags || [])
respond_with @all_tags
end
end

View File

@ -0,0 +1,2 @@
module TagsHelper
end

View File

@ -1,8 +1,35 @@
:javascript
angular.module('timeoverflow').controller('TypeaheadCtrl',
function($scope, $http) {
$scope.selected = undefined;
$scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
// Any function returning a promise object can be used to load values asynchronously
$scope.getTags = function(val) {
return $http.get('/tags/index').then(function(res){
var tags = [];
angular.forEach(res.data, function(item){
tags.push(item);
});
return tags;
});
};
});
= simple_form_for(post) do |f|
= f.input :title
= f.input :description
= f.association :category
= f.input :tag_list, as: :string, placeholder: 'search tag', input_html: { class: 'typeahead', autocomplete: 'on' }
= f.input :tag_list, as: :string, placeholder: 'search tag', input_html: { class: "form-control", "ng-controller" => "TypeaheadCtrl", "ng-model" => "asyncSelected", "typeahead" => "tag for tag in getTags($viewValue) | filter:$viewValue | limitTo:8" , "typeahead-loading" => "loadingTags"}
%i{"ng-show" => "loadingTags", :class => "glyphicon glyphicon-refresh"}
- if current_user.manages? current_organization
= f.association :user, collection: current_organization.users

View File

@ -0,0 +1,7 @@
%h1 Tags#index
- @all_tags.each do |tag|
%h5
= tag
%span.glyphicon.glyphicon-tag

View File

@ -1,4 +1,6 @@
Timeoverflow::Application.routes.draw do
get 'tags/index'
devise_for :users
ActiveAdmin.routes(self)
@ -46,4 +48,6 @@ Timeoverflow::Application.routes.draw do
post :accept
end
resource :tags, only: [:index]
end

View File

@ -0,0 +1,12 @@
require 'spec_helper'
describe TagsController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end

View File

@ -0,0 +1,15 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the TagsHelper. For example:
#
# describe TagsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
describe TagsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe "tags/index.html.haml" do
pending "add some examples to (or delete) #{__FILE__}"
end

File diff suppressed because it is too large Load Diff