我刚开始使用angularjs,我正在努力将一些旧的JQuery插件转换为Angular指令.我想为my(element)指令定义一组默认选项,可以通过在属性中指定选项值来覆盖它.
我已经浏览了其他人的方式,在angular-ui库中,ui.bootstrap.pagination似乎做了类似的事情.
首先,所有默认选项都在常量对象中定义:
.constant('paginationConfig', {
itemsPerPage: 10,
boundaryLinks: false,
...
})
Run Code Online (Sandbox Code Playgroud)
然后将getAttributeValue实用程序函数附加到指令控制器:
this.getAttributeValue = function(attribute, defaultValue, interpolate) {
return (angular.isDefined(attribute) ?
(interpolate ? $interpolate(attribute)($scope.$parent) :
$scope.$parent.$eval(attribute)) : defaultValue);
};
Run Code Online (Sandbox Code Playgroud)
最后,这在链接函数中用于读入属性
.directive('pagination', ['$parse', 'paginationConfig', function($parse, config) {
...
controller: 'PaginationController',
link: function(scope, element, attrs, paginationCtrl) {
var boundaryLinks = paginationCtrl.getAttributeValue(attrs.boundaryLinks, config.boundaryLinks);
var firstText = paginationCtrl.getAttributeValue(attrs.firstText, config.firstText, true);
...
}
});
Run Code Online (Sandbox Code Playgroud)
对于想要替换一组默认值的标准内容,这似乎是一个相当复杂的设置.还有其他方法可以做到这一点吗?或者getAttributeValue以这种方式总是定义一个实用程序函数(如和解析选项)是否正常?我很想知道人们对这项共同任务采取了哪些不同的策略.
另外,作为奖励,我不清楚为什么interpolate需要这个参数.
javascript options default-value angularjs angularjs-directive
好的,StackOverflow上有很多关于此的帖子,但没有一个在解决方案上特别清楚.我想UIView用随附的xib文件创建一个自定义.要求是:
UIViewController- 一个完全独立的类我目前的做法是:
覆盖 -(id)initWithFrame:
-(id)initWithFrame:(CGRect)frame {
self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil] objectAtIndex:0];
self.frame = frame;
return self;
}
Run Code Online (Sandbox Code Playgroud)-(id)initWithFrame:在我的视图控制器中以编程方式实例化
MyCustomView *myCustomView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view insertSubview:myCustomView atIndex:0];
Run Code Online (Sandbox Code Playgroud)这工作正常(虽然从来没有调用[super init],只是使用加载的笔尖的内容设置对象似乎有点怀疑 - 这里有建议添加一个子视图在这种情况下也工作正常).但是,我希望能够从故事板中实例化视图.所以我可以:
UIView在故事板中放置父视图MyCustomView覆盖-(id)initWithCoder:- 我见过的代码最常适合以下模式:
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initializeSubviews];
}
return self;
}
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; …Run Code Online (Sandbox Code Playgroud)我试图在UICollectionView使用子UICollectionViewFlowLayout类的类中自定义标题的位置(基于堆叠标题的代码松散地显示在此处输入链接描述).
作为最小测试,假设我只想在所有标题的位置添加固定偏移量:
layoutAttributesForElementsInRect以便始终处理所有标题(这可能是问题的原因,我不确定)layoutAttributesForSupplementaryViewOfKind完整的实施包含在本文末尾.
(顺便说一下,我知道在第一步中添加所有标题,包括那些在rect之外的标题,并不是严格说来,但这是一个简单的例子,说明我希望制作的位置更复杂,这会导致所有标题在绘制矩形中显示.)
但是,当我运行代码时,我得到以下内容NSInternalInconsistencyException:
2014-01-15 00:41:50.130 CollectionStackedHeaders[60777:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'layout attributes for supplementary item at index path (<NSIndexPath: 0x8a7db90> {length = 2, path = 0 - 0})
changed from <UICollectionViewLayoutAttributes: 0x8a7f8b0> index path: (<NSIndexPath: 0x8a7d9c0> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 320 50);
to <UICollectionViewLayoutAttributes: 0x8a7fb80> index path: (<NSIndexPath: 0x8a7db90> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此处概述的方法将C++类包装在matlab mex包装器中.基本上,我有一个初始化mex文件,它返回一个C++对象句柄:
handle = myclass_init()
Run Code Online (Sandbox Code Playgroud)
然后我可以将它传递给另一个myclass_amethod使用句柄调用类方法的mex文件(例如),然后最终myclass_delete释放C++对象:
retval = myclass_amethod(handle, parameter)
myclass_delete(handle)
Run Code Online (Sandbox Code Playgroud)
为了便于使用,我把它包装在一个MATLAB类中:
classdef myclass < handle
properties(SetAccess=protected)
cpp_handle_
end
methods
% class constructor
function obj = myclass()
obj.cpp_handle_ = myclass_init();
end
% class destructor
function delete(obj)
myclass_delete(obj.cpp_handle_);
end
% class method
function amethod(parameter)
myclass_amethod(obj.cpp_handle_, parameter);
end
end
end
Run Code Online (Sandbox Code Playgroud)
这在非并行代码中工作正常.但是,只要我从一个内部调用它parfor:
cls = myclass();
parfor i = 1:10
cls.amethod(i)
end
Run Code Online (Sandbox Code Playgroud)
我得到一个段错误,因为类的副本是在parfor循环中(在每个worker中)制作的,但由于每个worker都是一个单独的进程,因此不会复制C++对象实例,从而导致指针无效.
我最初尝试检测每个类方法何时在parfor循环中运行,并且在这些情况下也重新分配C++对象.但是,由于无法检查是否已为当前工作程序分配了对象,因此会导致多次重新分配,然后只有一次删除(当工作人员退出时)导致内存泄漏(请参阅附录中的附录)问题详情).
matlab.mixin.Copyable在C++中,处理它的方法是复制构造函数(这样只有在复制包装器MATLAB类时才重新分配C++对象).快速搜索会显示matlab.mixin.Copyable类类型,它似乎提供了所需的功能(即MATLAB句柄类的深层副本).因此,我尝试了以下方法:
classdef myclass …Run Code Online (Sandbox Code Playgroud) 我有一个对象,其中包含我在init方法中设置的一组自定义属性.在某些时候,我想将对象重置为原始状态.在Apple的文档中声明,init永远不应该单独调用alloc,因此使用ARC,这对我来说是一个非常合理的方式:
MyObject* myObject = [[MyObject alloc] init]; // inits myStringProperty to @""
myObject.myStringProperty = @"Hello world!";
NSLog(myObject.myStringProperty);
myObject = [[MyObject alloc] init]; // resets myStringProperty back to @""
NSLog(myObject.myStringProperty);
Run Code Online (Sandbox Code Playgroud)
但是,我已经读过(例如这里)在这种情况下的一个常见模式是显式定义一个reset方法并将重新初始化代码重构到那里.显然,这避免了必须创建一个全新的对象(虽然我无法想象它的开销是如此之高,并且将释放和重新分配给ARC似乎是最简单的方法)但是还有其他任何理由选择一个方法超过另一个?对象重新初始化的一般Objective C模式是什么?
我一直在尝试对我的CherryPy Web服务器进行性能分析,但是该文档缺少如何设置的详细信息。我了解我应该能够cherrypy.lib.profiler用作中间件来安装我的初始服务器。现在,我有如下代码:
server_app = ServerClass()
cherrypy.tree.mount(server_app, '/', '/path/to/config/file.cfg')
cherrypy.engine.start()
cherrypy.engine.block()
Run Code Online (Sandbox Code Playgroud)
我想挂载性能分析中间件,似乎需要以下内容:
from cherrypy.lib import profiler
server_app = ServerClass()
server_cpapp = cherrypy.Application(server_app, '/', '/path/to/config/file.cfg')
server_profile_cpapp = profiler.make_app(server_cpapp, '/home/ken/tmp/cprofile', True)
#cherrypy.tree.mount(server_profile_cpapp)
cherrypy.tree.graft(server_profile_cpapp)
cherrypy.engine.start()
cherrypy.engine.block()
Run Code Online (Sandbox Code Playgroud)
出于某种原因,cherrypy.tree.mount它不起作用,但是,如果我使用cherrypy.tree.graft全部,似乎一切正常(我可以像往常一样向服务器发出请求)
但是,上面的代码cp_0001.prof在下面生成一个文件/home/ken/tmp/cprofile,我不确定如何解释它。我尝试使用pyprof2calltree将数据读取到KCacheGrind中,但是遇到解析错误。我正在做的事情看起来是否正确,如果可以,我该如何解释输出文件?
我有一个类/结构如下:
struct MyStruct {
std::string member_one;
std::string member_two;
};
Run Code Online (Sandbox Code Playgroud)
我创建了一个长度为vector的向量MyStruct,其成员设置为自定义值:std::vector<MyStruct>N
std::vector<MyStruct> my_struct_vect(10);
// initialize class instances
Run Code Online (Sandbox Code Playgroud)
现在我想将第一个成员提取memberOne到一个新的向量中.我可以这样做:
std::vector<std::string> member_one_vect(my_struct_vect.size());
for (size_t i = 0; i < my_struct_vect.size(); ++i) {
member_one_vect[i] = my_struct_vect[i].member_one;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有更快/更优雅/更清洁的方式,而无需每次都编写自定义循环?例如,在Python中,我可以通过理解很容易地做到这一点.我不希望在C++中有类似的东西,但我想知道是否有某种方法可以简化这一点.
感谢使用std::transform和使用的响应boost::adaptors::transformed.这些非常有用,但为了紧凑,值得注意的是它们依赖于C++ 11中引入的lambda函数(可以不使用它们,但这需要定义一个单独的辅助函数).
那么对于奖励积分,有没有办法在C++ 03中以紧凑的方式做到这一点?
ios ×2
objective-c ×2
angularjs ×1
c++ ×1
cherrypy ×1
ios7 ×1
javascript ×1
matlab ×1
mex ×1
middleware ×1
nib ×1
object ×1
oop ×1
options ×1
profiling ×1
python ×1
stl ×1
storyboard ×1
uiview ×1