标签: controller

Backbone.js控制器中的默认路由?

我想为我的backbone.js控制器设置一个默认路由.目前我这样做:

class DealSearchController extends Backbone.Controller
    routes:
        'list' : 'showListView'
        'photos' : 'showPhotoView'
        'map' : 'showMapView'

    initialize: ->
        ....        
            window.location.hash = 'list' if ! _.include( _.keys(@routes),(window.location.hash || '').replace('#',''))
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?

controller backbone.js

45
推荐指数
1
解决办法
2万
查看次数

Angular JS可调整大小的div指令

我的网站将有多个部分,每个部分我打算可以调整大小.为了实现这一点,我制定了一个"可调整大小"的指令,例如:

<div class="workspace" resize="full" ng-style="resizeStyle()">
<div class="leftcol" resize="left" ng-style="resizeStyle()">
Run Code Online (Sandbox Code Playgroud)

使用类似于以下内容的指令:

lwpApp.directive('resize', function ($window) {
    return {
        scope: {},

        link: function (scope, element, attrs) {
            scope.getWinDim = function () {
                return {
                    'height': window.height(),
                    'width': window.width()
                };
            };

            // Get window dimensions when they change and return new element dimensions
            // based on attribute
            scope.$watch(scope.getWinDim, function (newValue, oldValue) {
                scope.resizeStyle = function () {
                    switch (attrs.resize) {
                    case 'full':
                        return {
                            'height': newValue.height,
                            'width': (newValue.width - dashboardwidth)
                        };

                    case 'left':
                        return …
Run Code Online (Sandbox Code Playgroud)

scope resize controller directive angularjs

45
推荐指数
2
解决办法
7万
查看次数

Angularjs:服务多个$ resource urls/data sources的服务?

我有一个Angular服务/提供程序,它为我的控制器提供json数据,效果很好:

    angular.module('myApp.services', ['ngResource']).
      factory("statesProvider", function($resource){
      return $resource('../data/states.json', {}, {
        query: {method: 'GET', params: {}, isArray: false}
      });
    });
Run Code Online (Sandbox Code Playgroud)

我还需要将json数据从另一个文件提供给同一个控制器counties.json.

我在哪里可以找到如何编写一个服务器来为我的控制器提供这两个文件?

controller angularjs

44
推荐指数
2
解决办法
3万
查看次数

codeigniter检查每个控制器中的用户会话

我在我的一个控制器中有这个私有会话,用于检查用户是否已登录:

function _is_logged_in() {

   $user = $this->session->userdata('user_data');

   if (!isset($user)) { 
      return false; 
   } 
   else { 
      return true;
   }

}
Run Code Online (Sandbox Code Playgroud)

问题是我有多个控制器.如何在其他控制器中使用此功能?在每个控制器中重新定义功能不是很"干".

有任何想法吗?

php methods controller codeigniter

41
推荐指数
3
解决办法
6万
查看次数

Rails:在CoffeeScript或JavaScript资产文件中访问控制器实例变量

在Rails 3.1中,无法使用诸如<%= @ foo%>之类的语法访问资产js.erb或coffee.erb文件中的控制器实例变量,其中@foo在控制器中设置.那么问题是将控制器变量传递给CoffeeScript或JavaScript资产的最佳方法是什么.

这个问题在论坛上有多种复杂形式的问题,但我再次提出这个问题的意思是要有一个所有建议聚集在一起的地方,并且所提供的代码简单易读.另请注意,我特指的是资产,而不是查看响应文件.

controller ruby-on-rails erb instance-variables coffeescript

41
推荐指数
3
解决办法
3万
查看次数

如何访问Spring休息控制器中的普通json主体?

拥有以下代码:

@RequestMapping(value = "/greeting", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String greetingJson(@RequestBody String json) {
    System.out.println("json = " + json); // TODO json is null... how to retrieve plain json body?
    return "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)

尽管json在正文中发送,但String json参数始终为null.

请注意,我不想要自动类型转换,我只想要普通的json结果.

这例如有效:

@RequestMapping(value = "/greeting", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String greetingJson(@RequestBody User user) {
    return String.format("Hello %s!", user);
}
Run Code Online (Sandbox Code Playgroud)

可能我可以使用ServletRequest或InputStream作为参数来检索实际的身体,但我想知道是否有更简单的方法?

rest spring controller spring-mvc

41
推荐指数
4
解决办法
6万
查看次数

将JSON数据传递给Spring MVC控制器

我需要向Spring MVC controller发送一个JSON字符串.但我没有任何表单绑定,我只需要向Controller类发送一个简单的JSON数据.我正在对Controller方法进行jQuery AJAX调用,如下面的代码.

$.ajax ({
    url: "./save",
    type: "POST",
    data: JSON.stringify(array),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(){
        alert("success ");
    }
});
Run Code Online (Sandbox Code Playgroud)

但是我如何在Controller方法中检索它?(注意:它只是简单的JSON数据而不是表单提交).

jquery json controller spring-mvc

41
推荐指数
2
解决办法
14万
查看次数

使用查询字符串参数进行Rails操作缓存

如何在我的操作具有查询字符串参数的情况下使用Rails缓存我的REST控制器?

Example: GET /products/all.xml?max_price=200
Run Code Online (Sandbox Code Playgroud)

谢谢!

caching controller ruby-on-rails

40
推荐指数
2
解决办法
2万
查看次数

将Grails日期与控制器中的params绑定

为什么通过grails控制器中的参数从视图中提取日期这么难?

我不想像这样用手提取日期:

instance.dateX = parseDate(params["dateX_value"])//parseDate is from my helper class
Run Code Online (Sandbox Code Playgroud)

我只是想用instance.properties = params.

在模型中,类型是java.util.Date和params中的所有信息:[dateX_month: 'value', dateX_day: 'value', ...]

我在网上搜索,没有发现任何事情.我希望Grails 1.3.0可以帮助但仍然是相同的.

我不能也不会相信手工提取日期是必要的!

data-binding grails groovy controller date

40
推荐指数
3
解决办法
3万
查看次数

如何在Rails 4中测试Controller关注点

在Rails 4控制器中使用时,处理问题测试的最佳方法是什么?说我有一个小问题Citations.

module Citations
    extend ActiveSupport::Concern
    def citations ; end
end
Run Code Online (Sandbox Code Playgroud)

测试中的预期行为是包含此关注点的任何控制器都将获得此citations端点.

class ConversationController < ActionController::Base
    include Citations
end
Run Code Online (Sandbox Code Playgroud)

简单.

ConversationController.new.respond_to? :yelling #=> true
Run Code Online (Sandbox Code Playgroud)

但是,孤立地测试这种问题的正确方法是什么?

class CitationConcernController < ActionController::Base
    include Citations
end

describe CitationConcernController, type: :controller do
    it 'should add the citations endpoint' do
        get :citations
        expect(response).to be_successful
    end
end
Run Code Online (Sandbox Code Playgroud)

不幸的是,这失败了.

CitationConcernController
  should add the citations endpoint (FAILED - 1)

Failures:

  1) CitationConcernController should add the citations endpoint
     Failure/Error: get :citations
     ActionController::UrlGenerationError:
       No route matches {:controller=>"citation_concern", …
Run Code Online (Sandbox Code Playgroud)

controller rspec ruby-on-rails activesupport-concern

40
推荐指数
3
解决办法
2万
查看次数