3天前,在阅读了很多关于使用OOP的优点之后,我开始使用类作为练习在OOP中重写我的一个脚本.
现在我很困惑天气我应该使用例外与否.他们似乎让我的工作更努力,更长久.
我的应用程序检查数据是否是通过Ajax请求发送的,然后通过脚本使用该信息.
检查此示例:
/*
* The older way
*/
if($ajaxEnabled) {
$error = errorWrap('Ajax error');
} else {
$error = errorWithBackLinkWrap('NoAjax error');
}
function doSomething() {
if(empty($POST['name'])) {
die($error);
}
}
/*
* OOP way
*/
class someClass {
private $_ajaxEnabled;
public function doSomething() {
try {
if(!$this->_isDateValid()) {
if($this->$_ajaxEnabled) {
throw new ajaxException('Ajax error');
} else {
throw new noAjaxException('NOAjaxError');
}
}
} catch(ajaxException $e) {
echo $e->getErrorMessage();
} catch(noAjaxException $e) {
echo $e->getErrorMessage();
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码仅用于演示问题,因此我知道其中有一些未定义的函数:). …
使用时AsyncTaskLoader
屏幕方向发生变化,我遇到了一些麻烦.让我给你一些关于我的应用程序结构的背景:我有一个非常简单的应用程序,它从一个URL获取结果并显示它.该应用程序由一个FragmentActivity
和三个组成Fragments
.我的三个片段如下:
将AsyncTaskLoader
被用于从任一个内容提供者加载数据(如果它是高速缓存),或从网络上.
一切都很好,直到屏幕上的变化!我查看了输出,LoaderManager.enableDebugLogging(true)
似乎问题的根源是LoaderCallbacks.onLoadFinished
在我的活动中没有被调用.
这是启动加载器的一段代码:
/* This function is defined in the "first" fragment in an interface and
* implemented in the activity */
@Override
public void onFormSubmission(String word) {
showLoadingFragment();
if ( mWord == null || mWord.equals(word) ) {
getSupportLoaderManager().initLoader(0, word, this);
} else {
getSupportLoaderManager().restartLoader(0, word, this);
}
// Store the word
mWord = word;
}
Run Code Online (Sandbox Code Playgroud)
显示结果的代码:
@Override
public void onLoadFinished(Loader<Bundle> loader, Bundle data) …
Run Code Online (Sandbox Code Playgroud) 嘿那里,我有一个阿拉伯语联系脚本,它使用Ajax在填写表单后从服务器检索响应.
在一些apache服务器上,jQuery.parseJSON()
抛出一个invalid json
异常的同一个json,它在其他服务器上完美解析.仅在chrome和IE上抛出此异常.
json内容使用php的json_encode()
函数进行编码.我尝试使用json数据发送正确的标头并将unicode设置为utf-8,但这没有帮助.
这是我尝试解析的json响应之一(删除了if的第二部分,因为它很长):
{"pageTitle":"\u062e\u0637\u0623 \u0639\u0646\u062f \u0627\u0644\u0625\u0631\u0633\u0627\u0644 !"}
注意:这个数据的语言是阿拉伯语,这就是为什么它在用php解析之后看起来像这样json_encode()
.
您可以尝试在给出的示例中发出请求,并使用firebug或webkit开发人员工具查看完整的响应数据.响应通过jsonlint!
最后,我有两个使用相同版本脚本的URL,尝试使用chrome或IE浏览它们以查看损坏示例中的错误.
工作示例:http://namodg.com/n/
破碎的例子:http://www.mt-is.co.cc/my/call-me/
更新:为了澄清更多,我想请注意,我通过使用旧eval()
解析内容来修复此问题,我发布了另一个版本,使用此修复程序,它是这样的:
// Parse the JSON data
try
{
// Use jquery's default parser
data = $.parseJSON(data);
}
catch(e)
{
/*
* Fix a bug where strange unicode chars in the json data makes the jQuery
* parseJSON() throw an error (only on some servers), …
Run Code Online (Sandbox Code Playgroud) 今天,当我为两个显示和隐藏菜单的方法编写代码时,我做了一个小测试,看看检查菜单可见性的最有效方法.
从浏览器到另一个浏览器的结果各不相同,FF 4.0b12的使用速度更快$.data
,但Chrome(webkit)和Opera在使用时速度更快$.is(':visible')
.
我无法在IE9上测试,因为浏览器一直锁定我!以下是测试用例:http://jsperf.com/data-or-display/3
那么,用jQuery检查可见性的最有效方法是什么?
我正试着用我的一个脚本去oop.这是一个联系脚本,通过jQuery的ajax函数发送电子邮件时处理编码.
我想让用户能够在同一页面中使用相同的脚本和两个表单,并使其成为一项简单的工作.
现在我已经制作了一个原型,它将如何用oop重写它.
这对我来说真的很困惑,但我每天都在学习.对我来说最困难的部分是我应该在哪里放置我的方法以及如何制作脚本流程.
为了演示我的意思,这里是我现在使用的代码的一些部分:
/*
* Start defining some vars at the runtime
*/
public function __construct($data, $config = array()) {
$lang = isset($config['language']) ? $config['language'] : 'en';
$this->_getPhrases($lang);
$this->_recieverEmail = isset ($config['reciever_email']) ? filter_var($config['reciever_email'], FILTER_SANITIZE_EMAIL) : die($this->_phrase['noRecieverEmail']);
$this->_ajax = ($this->_enableAjax($config['ajax_enabled'])) ? true : false;
$this->_data = isset($data) ? (is_array($data) ? $data : die($this->_phrase['errors']['dataNotArray'])) : $_POST;
}
/*
* Send the message
*/
public function send() {
if(!$this->isDataVaild($this->_data)) {
return false;
}
$this->_data = $this->_cleanData($this->_data);
$this->setSenderName($this->_data['name']);
$this->setSenderEmail($this->_data['email']);
$this->_message = …
Run Code Online (Sandbox Code Playgroud) 今天我使用jQuery为share-icons制作了一个效果.效果有点复杂,所以我试着想出一种优化性能的方法.我最终将$(this)
对象缓存到数组中.
我使用数组缓存对象上传了一个效果的工作示例(将鼠标悬停在图标上以查看效果):http: //mahersalam.co.cc/addthis/
<div id="share-widget" class="addthis_toolbox">
<a class="addthis_button_favorites" title="??? ???????"><div>??? ???????</div></a>
<a class="addthis_button_facebook" title="???? ?? ??????"><div>???? ?? ??????</div></a>
<a class="addthis_button_twitter" title="???? ?? ?????"><div>???? ?? ?????</div></a>
<a class="addthis_button_email" title="???? ?????? ????????"><div>???? ?????? ????????</div></a>
<a class="addthis_button_compact" title="???? ??? ??????? ?????? ?? ????? ????????"><div>?????? ?? ???????</div></a>
</div>
Run Code Online (Sandbox Code Playgroud)
// Return jQuery-obj of the share links
var shareLinks = $('#share-widget').find('a').css('opacity', 0.8);
//////////////////////////////////////////
// Only jQuery way
//////////////////////////////////////////
shareLinks.hover(
function () {
$(this).clearQueue()
.siblings()
.stop(true,false).fadeTo('fast', 0.3)
.end()
.stop(true, true).fadeTo('normal', …
Run Code Online (Sandbox Code Playgroud) 我有两个ActiveRecord
有HABTM关联的模型.
我想写一个scope
使用Arel来获取孤儿记录.
我的问题是我找不到一种方法来检索arel_table
关联.由于关系是HABTM,因此没有可以调用的模型arel_table
.
我现在有以下(有效),但我创建了一个新的arel表,其中包含连接表的名称(使用该reflect_on_association
方法检索).
scope :orphans, lambda {
teachers = arel_table
join_table = Arel::Table.new(reflect_on_association(:groups).options[:join_table])
join_table_condition = join_table.project(join_table[:teacher_id])
where(teachers[:id].not_in(join_table_condition))
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下SQL:
SELECT `teachers`.*
FROM `teachers`
WHERE (`teachers`.`id` NOT IN (SELECT `groups_teachers`.`teacher_id`
FROM `groups_teachers` ))
Run Code Online (Sandbox Code Playgroud)
那么有没有更好的方法来检索arel_table
而不是创建一个新的?
我有一个相当简单的类,看起来像这样:
class Person {
public:
Person(string name): _name(name) {};
void greet(const Person& person) const {
cout << "Hello, " << person._name << "!" << endl;
};
private:
string _name;
};
Run Code Online (Sandbox Code Playgroud)
请注意,该greet
方法采用该Person
类型的参数.当我传递一个Person
对象时,它按预期工作.现在让我们string
以这种方式将它作为参数传递给它:
Person maher("maher");
maher.greet("sam");
Run Code Online (Sandbox Code Playgroud)
当试图在QT中运行该代码时(在运行ubuntu的机器上),它会生成以下错误:
no matching function for call to ‘Person::greet(const char [4])’
我能够通过以这种方式转换字符串来解决此错误: maher.greet(string("sam"));
我的问题如下:为什么c ++ '看不到'我将字符串传递给greet
方法?它与greet
方法接受Person
对象的事实有什么关系吗?
我试图拦截我的rails(3.0.10)应用程序中的消息来修改正文.虽然我能够找到一些关于如何做到这一点的信息,但似乎已经发生了变化,现在使用旧方法已经不再适用了.
我使用的代码如下所示:
class Hook
def self.delivering_email(message)
message.subject = 'Hook changed the subject'
end
end
ActionMailer::Base.register_interceptor(Hook)
Run Code Online (Sandbox Code Playgroud)
发送电子邮件后,主题不会更改!
我还发现了一条推文,指出deliver
在消息上使用方法时没有调用拦截器,但是premailer-rails3 gem使用了我使用的相同方法并且它在那里工作(该插件特别提到它适用于该deliver
方法)!
我在这里没有想法,所以是什么导致了我的问题?
javascript ×3
jquery ×3
php ×3
oop ×2
actionmailer ×1
activerecord ×1
android ×1
arel ×1
c++ ×1
casting ×1
coding-style ×1
json ×1
optimization ×1
performance ×1
sql ×1